我想填写注册表,但脚本无法按照我想要的方式工作

时间:2018-10-31 17:56:59

标签: javascript

要验证检查点,如果

,表单将必须显示警报
  • 输入之一为空
  • 密码少于8个字符
  • 没有有效的电子邮件地址
  • 密码必须是字符,数字和至少一个大写字母的组合
  • 最后,重置按钮会将所有输入重置为空:

    //Variable declaration
    
    var username=document.forms["Registration"]["name"];
    var e_mail=document.forms["Registration"]["email"];
    var password=document.forms["Registration"]["psw1"];
    var passwordcheck=document.forms["Registration"]["psw2"];
    
    //add eventListener
    username.addEventListener("blur", NameVerify, true);
    e_mail.addEventListener("blur", EmailVerify, true);
    password.addEventListener("blur", PasswordVerify, true);
    passwordcheck.addEventListener("blur", PasswordVerify, true);
    
    // validate the registration   
    function Validate(){
        if (username.value=="")
        {
            alert("username is required");
            username.focus()
            return false;
        }
        if (e_mail.value=="")
        { 
           alert("Email is required");
           e_mail.focus()
           return false;
        }
        if (password.value=="")
        {
           alert("Password is required");
           password.focus()
           return false;
        }
        if (passwordcheck.value=="")
        {    
           alert("Re-enter your password");
           passwordcheck.focus()
           return false;
        }
        if(password.value != passwordcheck.value){
          alert("Password do not match!!")
          passwordcheck.focus()
          return false;
        }
    }
    
    //check the username value
    function NameVerify(username){
        if (username.value !=0) {
            document.querySelector.backgroundColor = lightGrey;
            return true;
        }
    }
    
    //check the e_mail
    function EmailVerify(e_mail){          
        if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.`\w{2,3})+$/.test(Registration.email.value))`
        {
             return (true)
        }
        alert("You have entered an invalid email address!")
        e_mail.focus()
        return (false)
    }
    
    //check the password
    function PasswordVerify(password){
        var psw = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,20}$/;
    
        if(password.value.match(psw)) 
        { 
             alert('Correct, try another...')
             return true;
        }
        else
        { 
             alert('Wrong!!')
             return false;
        }        
    }
    
    // clear all text inputs when the page is loaded
    function clearInp() {
        document.getElementsByTagName("input").value = "";
        return true;
    }
    
    //reset all text fields
    function Reset() {
        document.querySelector("#Registration").reset();
        return true;
    }
    

1 个答案:

答案 0 :(得分:1)

所有这些都不需要任何JavaScript。

  • 输入之一为空
<input type="text" required />
  • 密码少于8个字符
<input type="password" minlength="8" />
  • 没有有效的电子邮件地址
<input type="email" />
  • 密码必须是字符,数字和至少一个大写字母的组合
<input type="password" pattern="(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{8,}" />
  • 最后,重置按钮会将所有输入重置为空
<input type="reset" value="Reset form" />

一旦您从表单中删除了所有JavaScript代码,您将发现您的表单不再有任何JavaScript错误;)