I want to validate name, email and zip code on a button click, but it's showing only last error message, not specific to the field. I am writing this code in JavaScript & jquery. Any help appreciated.
if (!ck_name.test(name)) {
errors[errors.length] = "You valid First Name .";
document.getElementById("errorname").innerHTML = "You must enter a username";
return false;
}
if (!ck_name.test(lname)) {
errors[errors.length] = "You valid last name .";
document.getElementById("errorlastname").innerHTML = "You must enter a username";
return false;
}
When you return a value in a function, none of the code following the return statement is executed. Because of this, rather than returning false after each validation error, you should should only return true/false after you've performed all of your desired validations.
if (!ck_name.test(name)) {
errors.push("You valid First Name.");
document.getElementById("errorname").innerHTML = "You must enter a username";
}
if (!ck_name.test(lname)) {
errors.push("You valid last name.");
document.getElementById("errorlastname").innerHTML = "You must enter a username";
}
return (errors.length == 0);
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.