Why does the output stop at the second if else?
Try removing
return true;
and add that at the end of the function. Other wise the below code will not execute when a return true is reached. Try the code below.
function validate(){
var a = document.getElementById("name").value;
var letters = /^[A-Za-z]+$/;
if(a==""){
document.getElementById("errName").innerHTML = "Please enter your full name!";
return false;
}
else if ((!a=="")&&(!a.match(letters))){
document.getElementById("errName").innerHTML = "Not valid!";
return false;
}
else{
document.getElementById("errName").innerHTML = "OK!";
}
var b = document.getElementById("regno").value;
var Number = /^[0-9]+$/;
var iChars = "!`@#$%^&*()+=-[]\\\';,./{}|\":<>?~_";
if(b==""){
document.getElementById("errRegNo").innerHTML = "Please enter your contact number!";
return false;
}
else if (!b.match(Number) && !b.match(iChars)){
document.getElementById("errRegNo").innerHTML = "Not valid!";
return false;
}
else if(b.match(Number) || b.match(iChars)){
document.getElementById("errRegNo").innerHTML = "OK!";
}
var c = document.getElementById("email").value;
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if(c==""){
document.getElementById("errEmail").innerHTML = "Please enter your contact number!";
return false;
}
else if ((!c=="")&&(c.match(mailformat))){
document.getElementById("errEmail").innerHTML = "Not valid!";
return false;
}
else{
document.getElementById("errEmail").innerHTML = "OK!";
}
var d = document.getElementById("uid").value;
if(d==""){
document.getElementById("errUid").innerHTML = "Please enter your user id!";
return false;
}
else if (!d==""){
document.getElementById("errUid").innerHTML = "OK!";
}
var e = document.getElementById("password").value;
var f = document.getElementById("confirm").value;
if(e==""){
document.getElementById("errPassword").innerHTML = "Please enter your password!";
return false;
}
else if (!e==f){
document.getElementById("errPassword").innerHTML = "Not valid!";
return false;
}
else{
document.getElementById("errPassword").innerHTML = "OK!";
}
if(f==""){
document.getElementById("errPassword").innerHTML = "Please confirm your password!";
return false;
}
else if (!f==e){
document.getElementById("errPassword").innerHTML = "Not valid!";
return false;
}
else{
document.getElementById("errPassword").innerHTML = "OK!";
}
return true;
}
Since you are using return
, in any case where a condition is matched in the starting part of your code, it will return true and thus won't execute the remaining code. For example:
function validate(){
var a = document.getElementById("name").value;
var letters = /^[A-Za-z]+$/;
if(a==""){
document.getElementById("errName").innerHTML = "Please enter your full name!";
//if this condition is matched, it will simply return true and it will exit the function.
return false;
}
else if ((!a=="")&&(!a.match(letters))){
document.getElementById("errName").innerHTML = "Not valid!";
return false;
}
else{
document.getElementById("errName").innerHTML = "OK!";
return true;
}
// this wont be executed.
var b = document.getElementById("regno").value;
var Number = /^[0-9]+$/;
var iChars = "!`@#$%^&*()+=-[]\\\';,./{}|\":<>?~_"