-1

我有一个表单验证脚本所有的验证工作。但是当有关密码不匹配的警报出现时,这些字段会清除吗?我怎样才能避免这种情况?

 <script type="text/javascript">

 function formValidator(){
// Make quick references to our fields
var FNAME = document.getElementById('FNAME');
var LNAME = document.getElementById('LNAME');
var EMAIL = document.getElementById('EMAIL');
var GENDER = document.getElementById('GENDER');
var ADDRESS = document.getElementById('ADDRESS');
var CONTACTNO = document.getElementById('CONTACTNO');
var PASSWORD = document.getElementById('PASSWORD');
var PASSWORD2 = document.getElementById('PASSWORD2');

// Check each input in the order that it appears in the form!
if(isAlphabet(FNAME, "Please enter only letters for your first name"))
{
    if(isAlphabet(LNAME, "Please enter only letters for last name"))
    {
        if(emailValidator(EMAIL, "Please enter a valid email address"))
        {
            if(madeSelection(GENDER, "Please Choose a Gender"))
            {
                if(isAlphanumeric(ADDRESS, "Numbers and Letters Only for Address"))
                {
                    if(isNumeric(CONTACTNO, "Please enter only numbers for Contact No."))
                    {
                        if(lengthRestriction(PASSWORD, 6))
                        {
                            if (PASSWORD2 != PASSWORD)
                            {
                            alert ("You did not enter the same new password twice. Please re-enter your password.");
                            return true;
                            }   
                        }
                    }
                }
            }
        }
    }
}

return false;

 }

我认为我的其他功能没有问题?

 function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
    alert(helperMsg);
    elem.focus(); // set the focus to this input
    return false;
}
return true;
 }

 function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression)){
    return true;
}else{
    alert(helperMsg);
    elem.focus();
    return false;
}
 }

function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
    return true;
}else{
    alert(helperMsg);
    elem.focus();
    return false;
}
 }

 function isAlphanumeric(elem, helperMsg){
var alphaExp = /^[0-9a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
    return true;
}else{
    alert(helperMsg);
    elem.focus();
    return false;
}
 }

 function lengthRestriction(elem, min){
var uInput = elem.value;
if(uInput.length >= min){
    return true;
}else{
    alert("Please enter atleast " +min+ " characters");
    elem.focus();
    return false;
}
}

function madeSelection(elem, helperMsg){
if(elem.value == "Please Choose"){
    alert(helperMsg);
    elem.focus();
    return false;
}else{
    return true;
}
 }

function emailValidator(elem, helperMsg){
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp)){
    return true;
}else{
    alert(helperMsg);
    elem.focus();
    return false;
}
 }


</script>

我想我做错了这部分??但我不知道如何解决?请帮忙。

if (PASSWORD2 != PASSWORD)
{
alert ("You did not enter the same new password twice. Please re-enter your password.");
return true;
}   
4

1 回答 1

0
Try to do something like:

    if (PASSWORD2.value == PASSWORD.value)
    {
      alert ("You did not enter the same new password twice. Please re-enter your password.");
      return true;
    }
} 
于 2012-09-01T05:19:11.120 回答