Javascript 只有一个警报框,而不是更多

Javascript 只有一个警报框,而不是更多,javascript,Javascript,这也许是个愚蠢的问题,但这是我第一年使用javascript之类的东西。 我得到了一些警报框,我想知道是否有可能只显示一个警报框(javascript)以及我希望它们做的所有事情。 当他们填写其中一个输入或按钮时,allert将只显示其他缺失的内容 (我收到第一个代码的警报。当我填写时,我收到下一个代码的警报,依此类推。我希望所有代码都在一个代码中。) /*验证名称*/ var n=document.forms[“check”][“name”].value; 如果(n==null | | n==

这也许是个愚蠢的问题,但这是我第一年使用javascript之类的东西。 我得到了一些警报框,我想知道是否有可能只显示一个警报框(javascript)以及我希望它们做的所有事情。 当他们填写其中一个输入或按钮时,allert将只显示其他缺失的内容

(我收到第一个代码的警报。当我填写时,我收到下一个代码的警报,依此类推。我希望所有代码都在一个代码中。)

/*验证名称*/
var n=document.forms[“check”][“name”].value;
如果(n==null | | n==“”)
{
提醒(“请填写您的姓名”);
返回false;
}
/*验证性别*/
if(document.getElementById('male')。选中)
{
} 
else if(document.getElementById('female')。选中)
{
} 
其他的
{
提醒(“请输入您的性别。”);
返回false;
}
/*验证电子邮件*/
var e=文件。表格[“检查”][“电子邮件”]。值;
var atpos=e.indexOf(“@”);
var dotpos=e.lastIndexOf(“.”);
如果(e==null | | e==“”)
{
提醒(“请填写您的电子邮件”);
返回false;
}
如果(atpos)


您好,您应该创建一个全局数组并在最后加入:)它工作得很好。唯一的问题是,“alerts.join”代码下的所有“return false”必须为1。像这样…警报(alerts.join(“,”);返回false;
/*validate name*/
var n=document.forms["check"]["name"].value;
if(n==null||n=="")
{
    alert("Please, fill in your name.");
    return false;
}

/*validate the sex*/
if(document.getElementById('male').checked) 
{
} 
else if(document.getElementById('female').checked)
{
} 
else
{
    alert("Please, enter your gender.");
    return false;
}

/*validate the E-mail*/
var e=document.forms["check"]["email"].value;
var atpos=e.indexOf("@");
var dotpos=e.lastIndexOf(".");
if(e==null||e=="")
{
    alert("Please, fill in your e-mail.");
    return false;
}
if(atpos<1 || dotpos<atpos+2 || dotpos+2>=e.length)
{
    alert("This isn't a valid e-mail address.");
    return false;
}

/*validate agreement*/
if(document.getElementById("I don't want my information to be part of this website.").checked) 
{
} 
else if(document.getElementById("I wish to be registered.").checked) 
{
} 
else if(document.getElementById("I wish to get the new content of this website.").checked) 
{
} 
else
{
    alert("Please, tell us what we can do with your information.");
    return false;
}

/*validate the terms*/
if(document.getElementById("yes").checked)
{

}
else if(document.getElementById("no").checked)
{
    alert("You have to agree with the terms.");
    return false;
}
else
{
    alert("Please, enter the terms.");
    return false;
}
// initialise an array to populate along the way
var alerts = [];

/*validate name*/
var n = document.forms[ "check" ][ "name" ].value;
if ( n == null || n == "" ) {
    // push message onto the array
    alerts.push( "Please, fill in your name." );
    return false;
}

/*validate the sex*/
if ( document.getElementById( 'male' ).checked ) {} else if ( document.getElementById( 'female' ).checked ) {} else {
    // push message onto the array
    alerts.push( "Please, enter your gender." );
    return false;
}

/*validate the E-mail*/
var e = document.forms[ "check" ][ "email" ].value;
var atpos = e.indexOf( "@" );
var dotpos = e.lastIndexOf( "." );
if ( e == null || e == "" ) {
    // push message onto the array
    alerts.push( "Please, fill in your e-mail." );
    return false;
}
if ( atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= e.length ) {
    // push message onto the array
    alerts.push( "This isn't a valid e-mail address." );
    return false;
}

// join up the array of messages, and alert the user...
alert(alerts.join(", "));
// initialise an array to populate along the way
var alerts = [];
// push messages onto the array
// (repeat this step for all messages)
alerts.push( "Any validation message" );
// join up the array of messages, and alert the user...
alert(alerts.join(", "));