I want to make a registration form but the script wont work the way i want

 //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;
    }

Hi, midouyah,

First of all, as much as I love Regular Expressions (and I do), the RegEx that you are using will fail many valid email addresses (specifically, any FQDN with a TLD longer than three characters, like .mobi or .travel or .name.)

And trying to use RegEx to make sure only certain characters are used is not as effective as using RegEx to explicitly deny certain characters. Typically, when checking passwords one should check for length; but some go to the extreme of making sure that in addition to length, there are at least one upper-case, one lower-case, one integer, and one special character. I usually just check for length.

One of the issues I noticed with your validation is: if (username.value !=0) Basically this is saying that if anything other than 0 is entered, fail it. Try if(!username.value.length), checking to see if the username has a length greater than 0.

HTH,

^ _ ^

2 Likes

What input types are you using as html5 has the ability to validate email
Try googling
html5 input type validation
If you want a more comprehensive test by all means do it hut the html5 validation means that the stupid inputs are already handled.

Hi @midouyah, although @WolfShade was already able to identify a couple of problems it would be helpful to clarify what actually is the way you want the script to work (and where it does not work as expected). ;-)

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.