-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 に答える