简体   繁体   中英

Can't get form validation in JS to work

so I have been learning basic JS and i can't get form validation to work for numbers. I will post a code snippet below. Please I am a noob so can I possibly have an answer that is easier to understand from someone who is just learning the language. The first three if statements work fine, but the fourth I have trouble with...

var checkbox = function() {
            var error = "";
            var firstname = document.getElementById("fn").value;
            var lastname = document.getElementById("ln").value;
            var email = document.getElementById("Email").value;
            var age = parseInt(document.getElementById("age").value);
            var address = document.getElementById("A").value;
            var phone = parseInt(document.getElementById("pn").value);

            if(firstname.length < 1){
                error = "Enter a valid first name!";



            }
            if(lastname.length < 1){
                error += "\nEnter a valid last name!";

            }
            if(email.length <1){
                error += "\nEnter a valid email!";


            }
            if(age.length < 1 ){
                error += "\nEnter a valid age!";

            }





            if(error.length){
                alert(error)
                return false;

            }


            return true;




        }   

you can try with this one

        if(firstname.trim() == ""){
            error = "Enter a valid first name!";
        }
        if(lastname.trim()==""){
            error += "\nEnter a valid last name!";

        }
        if(email.trim() ==""){
            error += "\nEnter a valid email!";

        }
        if(age.trim() == "" ){
            error += "\nEnter a valid age!";

        }

        if(error.length>0){
            alert(error)
            return false;
        }

What are you checking your age field against on? Are you checking for its presence? if so, you can do this:

if(!age) {
    error += "\nThis field is required";
}

If you are checking if the value in the age field is a number, you can do this:

if(typeof age !== "number") {
    error += "\nPlease enter a valid number!";
}

If you are checking if the age field is under a certain age range you can do this:

if(age !== 30) {
    error += "\nYou are under 30!";
}

You should check the length of document.getElementById("age").value before call parseInt . Otherwise variable age can be NaN . For example:

var ageVal = document.getElementById("age").value;
var age;
if (ageVal){ //same as ageVal.length > 0
  age = parseInt(age);
  if (!age){// it can be NaN
    error += "\nEnter a valid age!";
  }
}
else
{
  error += "\nEnter an age!"; //here we have age not set
}

Please try this

if(isNaN(age)){
            error += "\nEnter a valid age!";

}

isNan() is an inbuilt javascript function. More information can be found at http://www.w3schools.com/jsref/jsref_isnan.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM