Javascript Registration Form Validation



  • Sorry I have been really struggling with this. I have more codes, but can't get pass the username yet. It wouldn't even send an alert. Below is just a portion of what I have. Thanks in advance. I just need suggestion on how to get first field (username) to validate and I can go from there.

    function validateForm() {
    
        var userId = document.myForm.userName.value;
        var password = document.myForm.passwordOne.value;
       var passwordVerify = document.myForm.passwordTwo.value;
        var fName = document.myForm.firstName.value;
        var lName = document.myForm.lastName.value;
        var emailAdd = document.myForm.email.value;
        var phone = document.myForm.phonenumber.value;
    
        if (uName(userId)) {
            if (validatePassword(password)) {
                if (passwordCheck(password, passwordVerify)) {
                    if (onlyLetters(fName)) {
                        if (onlyLetters(lName)) {
                            if (validateEmail(emailAdd)) {
                                if (phonenumber(phone)) {
                                }
                            }
                        }
                    }
                }
            }
        }
        return false;
    }
    
    
    function uName(userId){
    
        var userId_len = userId.value.length;
        var alphaNumber = /^[\w ]+$/;
    
        if (userId_len == 0)
        {
            alert("User Id should not be empty");
            userID.focus();
            return false;
        }
        else if (userId.value.match(alphaNumber)) {
            return true;
        }
        else {
            alert("User Id should be alphanumeric");
            userId.focus();
            return false;
        }
        }


  • Hey @willaeng

    I would start off by not calling on value twice. You call on value here:

    var userId = document.myForm.userName.value;

    and again here:

    var userId_len = userId.value.length;
    ...
    else if (userId.value.match(alphaNumber)) {
    

    Try removing the extra calls on .value and then go from there.



  • Hey @willaeng

    I would start off by not calling on value twice. You call on value here:

    var userId = document.myForm.userName.value;

    and again here:

    var userId_len = userId.value.length;
    ...
    else if (userId.value.match(alphaNumber)) {
    

    Try removing the extra calls on .value and then go from there.