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);
}
//-->
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);
}
//-->