In my project I have to validate all fields when the form is submitted and display error message on same page in order list above the form field. below is my code but when click enter the data, only one error display at a time. I try multiple solution but not properly work so how do I display multiple error at same time.
function validate() {
var minnumberofchars = 6;
var maxnumberofchars = 16;
var regularexperssion = /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{6,16}$/;
var letters = /^[A-Za-z]+$/;
var emailRegex = /^[A-Za-z0-9._]*\@[A-Za-z]*\.[A-Za-z]{2,5}$/;
var frname = document.getElementById('fname').value;
var lrname = document.getElementById('lname').value;
var fage = document.getElementById('age').value;
var femail = document.getElementById('email').value;
var fpassword = document.getElementById('password').value;
if (frname == "") {
document.getElementById('error').innerHTML = "enter the first name";
return false;
}
if (!letters.test(frname)) {
document.getElementById('error').innerHTML = "enter only text";
return false;
}
if (lrname == "") {
document.getElementById('error').innerHTML = "enter the last name";
return false;
}
if (!letters.test(lrname)) {
document.getElementById('error').innerHTML = "enter only text";
return false;
}
if (fage == "") {
document.getElementById('error').innerHTML = "enter the age";
return false;
}
if (isNaN(fage)) {
document.getElementById('error').innerHTML = "enter the only number";
return false;
}
if (femail == "") {
document.getElementById('error').innerHTML = "enter the email";
return false;
}
if (!emailRegex.test(femail)) {
document.getElementById('error').innerHTML = "enter the valid email";
return false;
}
if (fpassword == "") {
document.getElementById('error').innerHTML = "enter the password";
return false;
}
if (fpassword.length < minnumberofchars || fpassword.length > maxnumberofchars) {
document.getElementById('error').innerHTML = "password should between 6 to 16 character";
return false;
}
if (!regularexperssion.test(fpassword)) {
document.getElementById('error').innerHTML = "password should contain atleast one number and one special character";
return false;
}
return true;
}
<a href="index.php">Home</a>
<form method="post" name="form1" action="add.php">
<table border="0">
<tr>
<td>First Name</td>
<td><input type="text" id="fname" name="fname"></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" id="lname" name="lname"></td>
</tr>
<tr>
<td>Age</td>
<td><input type="text" id="age" name="age"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" id="email" name="email"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" id="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="ADD" name="submit" onclick="return validate()"></td>
</tr>
</table>
</form>
<div id='error' style='color:red'></div>
Enter password, Enter valid passwordLastly NEVER call anything in a forname="submit"- it will hide the submit event handler. Move thereturn validate()to the onsubmit of the form tag