Form Validation Problem

A

AnnMarie

Can someone let me know why my phone validation doesn't work within my
validate function. It works alone, but not within this function like
the other validations do.

Phone is not required, but if the user enters a phone number it must
be in the form of 555-555-5555.

Also, my submit confirmation dialog box comes up first and
continuously. I want it to appear last and once before final valid
submission.

Please let me know of a correct and better way of validating these
required fields: name, company, email, and validating phone for
correct format if user fills in phone number.

The code is attached and and the link is below.
Thanks.

http://matrix.csis.pace.edu/~f03-it604-s03/project/comments.html

<script language="JavaScript" type="text/javascript">
<!--
function validate(theForm)
{
var validity = true; // assume valid

if(frmComments.name.value=='' && validity == true)
{
alert('Your full name is required. Please enter your full
name!');
validity = false;
frmComments.name.focus();
event.returnValue=false;
}

if(frmComments.company.value=='' && validity == true)
{
alert('Your company name is required. Please enter the name of
your company!');
validity = false;
frmComments.company.focus();
event.returnValue=false;
}

var pattern1 = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
if (!pattern1.test(frmComments.email.value) && validity == true)
{
alert("Invalid E-mail Address! Please re-enter.")
frmComments.email.focus();
validity = false;
return false;
}

if(frmComments.zip.value == "" )
return true;
else
{
var pattern2 = /\d{5}/;
if(pattern2.test(frmComments.zip.value))
return true;
else
{
if(!pattern2.test(frmComments.zip.value) && validity == true)
{
alert("Invalid Zip Code! Must be in form 12345. Please
re-enter.");       
frmComments.zip.focus();
validity = false;
return false;
}
}
}

if(frmComments.phone.value == "")
return true;
else
{
var pattern3 = /\d{3}\-\d{3}\-\d{4}/;
if(pattern3.test(frmComments.phone.value))
return true;
else
{
if(!pattern3.test(frmComments.phone.value)&& validity == true)
{
alert("Invalid Phone Number! Must be in form 555-555-5555.
Please re-enter.");       
frmComments.phone.focus();
validity = false;
return false;
}
}
}

}


function formSubmit()
{
window.event.returnValue = false;
if ( confirm ( "Are you sure you want to submit?" ) )
window.event.returnValue = true;
}

function formReset()
{
window.event.returnValue = false;
if ( confirm( "Are you sure you want to reset?" ) )
{
frmComments.name.focus();
window.event.returnValue = true;
}
}
// -->
</script>

<form id="frmComments"
action="http://matrix.csis.pace.edu/~f03-it604-s03/cgi-bin/comments.pl"
method="post" onSubmit="return validate(this);" onReset="return
formReset(this)">


<input type="submit" align="right" value="Submit" name="submit"
onclick="formSubmit()">
 
L

Lee

AnnMarie said:
Can someone let me know why my phone validation doesn't work within my
validate function. It works alone, but not within this function like
the other validations do.

I can see a couple of problems, but most of the code is
unreadable. Please reformat it to fit in under 80
columns and post it again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top