469,623 Members | 1,365 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

Form Validation Problem

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-it6.../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()">
Jul 20 '05 #1
1 3643
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.

Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Stefan Richter | last post: by
6 posts views Thread by Darren | last post: by
9 posts views Thread by julie.siebel | last post: by
7 posts views Thread by h7qvnk7q001 | last post: by
27 posts views Thread by Chris | last post: by
11 posts views Thread by Rik | last post: by
4 posts views Thread by Greg Scharlemann | last post: by
5 posts views Thread by lucyh3h | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.