I have a validateFunction on submit which checks to see if Name and Phone Number
are entered or not. (Phone is optional so it checks only if its filled)
The Name validation works perfect but the number does not.
I read on the forum something about any thing being called after first return will not be executed and I do not understand it.
Can some one help make this code work?
function validateForm()
{
var x=document.forms["enterGuest"]["guestName"].value;
if (x==""||x==Null)
{
alert("Guest Name Please");
return false;
}
var c=document.forms["enterGuest"]["guestNum"].value;
if(c=="" || c==Null)
{
// Do nothing. Guest does not want to share the phone number.
alert(" This Guest Cannot Receive A Call.");
return true;
}
else
{
c.replace(/[^0-9]/g, '');
if (c.length !=10)
{
alert("10 Digits Please.");
return false;
}
else
{
var r = confirm(" Please Confirm The Phone Number Is Correct.");
if(r==true)
{
return true;
}
else
{
return false;
}
}
}
You can also use if/else statements to avoid having to use too many returns in the javascript.
Building on Nivas’s fiddle, example here –> http://jsfiddle.net/XWLXn/1/