【问题标题】:re-enter email validator not working on javascript重新输入电子邮件验证器不适用于 javascript
【发布时间】:2020-08-09 02:28:43
【问题描述】:

所以我正在尝试检查用户是否使用 javascript 在注册表单中两次输入了相同的电子邮件地址和密码。如果他们不匹配,它不应该让他们注册,但是只有部分密码有效并且电子邮件无效。这是我的代码:

<form>
           
    <input type="email" name="fname" placeholder="Email Adress" required="required" class="input-txt" id="email1">
    <input type="email" name="fname" placeholder="Confirm Email Adress" required="required" class="input-txt" id="email2"> 
    <br><br>
    <input id="password" type="password" name="fname" placeholder="Create Password" required="required" class="input-txt">
    <input id="confirm_password" type="password" name="fname" placeholder="Confirm Password" required="required" class="input-txt"><br>
    <button type="submit" class="btn">Sign up</button>
</form>

<script>
function validateMail() {
    if(first_email.value != second_email.value) {
        email2.setCustomValidity("Emails Don't Match");
    } else {
        email2.setCustomValidity('');
    }
}
email1.onchange = validateMail;
email2.onkeyup = validateMail;

var password = document.getElementById("password")
  , confirm_password = document.getElementById("confirm_password");

function validatePassword(){
    if(password.value != confirm_password.value) {
        confirm_password.setCustomValidity("Passwords Don't Match");
    } else {
        confirm_password.setCustomValidity('');
    }
}

password.onchange = validatePassword;
confirm_password.onkeyup = validatePassword;
</script>

【问题讨论】:

    标签: javascript validation


    【解决方案1】:

    您需要在脚本顶部指定var

     var first_email = document.getElementById("email1")
      , second_email = document.getElementById("email2")
      , password = document.getElementById("password")
      , confirm_password = document.getElementById("confirm_password");
    

    【讨论】:

    • 告诉我为什么会很有用!
    • @RiggsFolly 她忘记在 js 中添加电子邮件的变量...不添加 var 无法比较...所以我要求她添加变量...
    • 完成了 :) 把它放在你的答案中,这样如果其他人发现这个问题和你的答案,他们就会知道你做了什么以及为什么
    猜你喜欢
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 2018-04-28
    • 1970-01-01
    • 2015-02-24
    • 2023-03-15
    相关资源
    最近更新 更多