Javascript电子邮件和密码验证

时间:2017-08-08 17:17:50

标签: javascript html asp.net

我创建了一个用户可以注册的注册页面,我正在尝试检查:电子邮件是否有效,电子邮件是否匹配,密码是否符合长度要求,以及密码是否匹配。但它不能正常工作。 html代码采用相同的形式

电子邮件表格行:

        <tr>
            <td></br>Email Address:</td>
        </tr>
        <tr>
            <td><input id="em100" type="email" name="email1" required>                
        </td>
        </tr>
        <tr>
            <td></br>Confirm Email Address:</td>
        </tr>
        <tr>
            <td><input id="em101" type="email" name="email2" required></td>
        </tr>

密码表行:

        <tr>
        <td></br>Password:</td>
        </tr>
        <tr>
            <td><input id="pw100" type="password" name="pass1" required></td>
        </tr>
        <tr>
            <td></br>Confirm Password:</td>
        </tr>
        <tr>
            <td><input id="pw101" type="password" name="pass2" required></td>
        </tr>

提交表格行:

        <tr>
            <td style="padding-top: 10px;"></br><input type="submit" value="Register" onclick="regValidation();"></td>
        </tr>

Javascript代码:

function regValidation()
{
document.getElementById('td100').innerHTML = "";
event.preventDefault ? event.preventDefault() : event.returnValue = false; 

var email1 = document.getElementById('em100').value;
var email2 = document.getElementById('em101').value;
var pass1 = document.getElementById('pw100').value;
var pass2 = document.getElementById('pw101').value;

if (document.getElementById('em100').checkValidity()) {
    if (email1 = email2) {
        if (pass1 = pass2) {
            if (pass1.length >= @minPass || pass1.length <= @maxPass) {
                return true && regSuccess();
            } else {
                document.getElementById('td100').innerHTML += "</br>Password must be @minPass - @maxPass characters.";
            }
        } else {
            document.getElementById('td100').innerHTML += "</br>Passwords do not match.";
        }
    } else {
        document.getElementById('td100').innerHTML += "</br>Emails do not match.";
    }
} else {
    document.getElementById('td100').innerHTML += "</br>Emails are not valid.";
}
}

function regSuccess()
{
    alert("Thank you for signing up!");
    document.getElementById('fm101').submit();
    return;
}

3 个答案:

答案 0 :(得分:1)

你需要进行比较,并且可以更容易地将ifs链接起来:

function getError(){     
 if(pass1 !== pass2) return "password missmatch";
 if(email1 !== email2) return "emails missmatch";
 if(pass1.length < min || pass1.length > max) return "password to long/short";
return false;
}

所以你可以做到

var error = getError();
if(!error){
  return alert(" all fine");
}
alert(error);

答案 1 :(得分:0)

在条件中,您要分配值:

 CELERY_ENABLE_REMOTE_CONTROL = False 
 CELERY_SEND_EVENTS = False

这应该是

if (pass1 = pass2)

答案 2 :(得分:-1)

1检查 if 语句 如果(EMAIL1 == EMAIL2) {}

pass1和pass2的相同内容 如果(PASS1 == PASS2) {}

2如果你把

<input type="email">

浏览器会自动为您执行电子邮件验证

3检查密码是否超过最大长度只需添加即可 输入标记的 maxlength 属性

<input type="password" maxlength="10">

这样可以省去编写额外的js代码