Click to See Complete Forum and Search --> : Form validation problem


Colm
08-06-2003, 12:10 AM
Can anyone show me a better way of validating my form.
I'm trying check that the first name field is all alphabetic it does this ok but submits the form before validating the other fields. Thanks Colm.



<!--
function Validator(theForm)
{
if (theForm.CardNo.value == ""){
alert("Please enter a value for the \"Credit Card Number\" field.");
theForm.CardNo.focus();
return (false);
}

if (theForm.CardNo.value.length < 10)
{
alert("Please enter at least 10 characters in the \"Credit Card Number\" field.");
theForm.CardNo.focus();
return (false);
}

if (theForm.CardNo.value.length > 20)
{
alert("Please enter at most 20 characters in the \"Credit Card Number\" field.");
theForm.CardNo.focus();
return (false);
}

var checkOK = "0123456789-";
var checkStr = theForm.CardNo.value;
var allValid = true;
var decPoints = 0;
var allNum = "";
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
allNum += ch;
}
if (!allValid)
{
alert("Please enter only digit characters in the \"Credit Card Number\" field.");
theForm.CardNo.focus();
return (false);
}

if (/^[^0-9\/><\.,\\!\^\$\*\+\?@#%&\(\);:\[\]\{\}=""']+$/.test(theForm.FirstName.value)){
return (true)
}
alert("Please enter a value for the First Name field.");
theForm.FirstName.focus();
return (false)

if (theForm.LastName.value == "")
{
alert("Please enter a value for the \"Last Name\" field.");
theForm.LastName.focus();
return (false);
}

if (theForm.BusinessPhone.value == "")
{
alert("Please enter a value for the \"Business phone\" field.");
theForm.BusinessPhone.focus();
return (false);
}

if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(theForm.Email.value)){
return (true)
}
alert("Invalid E-mail Address! Please re-enter.")
return (false)
return (true);
}
//-->

moreta
08-06-2003, 02:17 PM
if (/^[^0-9\/><\.,\\!\^\$\*\+\?@#%&\(\);:\[\]\{\}=""']+$/.test(theForm.FirstName.value)){
return (true)
}
alert("Please enter a value for the First Name field.");
theForm.FirstName.focus();
return (false)



After you check the first name, the function returns true or false. Eiither way...it returns.

moreta

Colm
08-06-2003, 07:39 PM
Ok so I have removed the (return true). Now the form skips the First name and email fields.



<!--
function Validator(theForm)
{

{if (/^[^0-9\/><\.,\\!\^\$\*\+\?@#%&\(\);:\[\]\{\}=""']+$/.test(theForm.FirstName.value))
{
alert("Please enter a value for the First Name field.");
theForm.FirstName.focus();
return (false)
}}
if (theForm.LastName.value == "")
{
alert("Please enter a value for the \"Last Name\" field.");
theForm.LastName.focus();
return (false);
}

if (theForm.BusinessPhone.value == "")
{
alert("Please enter a value for the \"Business phone\" field.");
theForm.BusinessPhone.focus();
return (false);
}

{if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(theForm.Email.value))

{
alert("Invalid E-mail Address! Please re-enter.")
return (false)
}}
return (true);
}
//-->

moreta
08-07-2003, 07:32 AM
I'm not going to evaluate the regular expression, but assume if it's true, the field is valid? This would be consistent with your original post:if (/^[^0-9\/><\.,\\!\^\$\*\+\?@#%&\(\);:\[\]\{\}=""']+$/.test(theForm.FirstName.value)){
return (true)
}
alert("Please enter a value for the First Name field.");
theForm.FirstName.focus();
return (false)


Your revised code returns false when the expression is true. (I've omitted the extra curly braces.) Try adding "==-1".
if (/^[^0-9\/><\.,\\!\^\$\*\+\?@#%&\(\);:\[\]\{\}=""']+$/.test(theForm.FirstName.value)==-1)
{
alert("Please enter a value for the First Name field.");
theForm.FirstName.focus();
return (false)
}

Does this address the result you are getting?

moreta