
December 20th, 2012, 01:12 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 2
Time spent in forums: 12 m 27 sec
Reputation Power: 0
|
|
|
Nesting issue while duplicating functions
Greetings,
I have a functioning javascript file here that checks to make sure all the values in a form are filled out. It also checks to make sure that the email address is in a valid format.
Code:
function regchk()
{
frm=document.myform;
if(frm.fname.value=="")
{
alert("Please enter your first and last name");
frm.fname.focus();
return false;
}
if(frm.lname.value=="")
{
alert("Please enter your first and last name");
frm.lname.focus();
return false;
}
if(frm.em.value=="")
{
alert("Please enter your email address");
frm.em.focus();
return false;
}
else
{
if(!emailInvalid(frm.em.value))
{
alert("Please enter a valid email address");
frm.em.focus();
return false;
}
}
if(frm.ptcity.value=="")
{
alert("Please enter your city");
frm.ptcity.focus();
return false;
}
if(frm.st.value=="")
{
alert("Please enter your state");
frm.st.focus();
return false;
}
if(frm.zip.value=="")
{
alert("Please enter your zip code");
frm.zip.focus();
return false;
}
if(frm.pn.value=="")
{
alert("Please enter your phone number");
frm.pn.focus();
return false;
}
if ( frm.agree.checked == false )
{
alert ( "You must agree to the Terms and Conditions and Privacy Policy to continue" );
return false;
}
}
function emailInvalid(s)
{
if(!(s.match(/^[\w]+([_|\.-][\w]{1,})*@[\w]{2,}([_|\.-][\w]{1,})*\.([a-z]{2,4})$/i) ))
{
return false;
}
else
return true;
}
I'd like to now modify it to also ensure that the phone number is 10 digits. I have been trying to use the following bit of code.
Code:
if(!phoneInvalid(frm.em.value))
{
alert("Please enter 10 digits for the phone number");
frm.pn.focus();
return false;
}
function phoneInvalid(s)
{
if(phone.length != 10) {
alert("please enter 10 digits for the phone number");
{
return false;
}
However, I can't seem to properly nest the code to make the entire script function. I have been placing it wrong.
Can anyone please modify the original code above to ensure the phone number field is a valid 10 digits?
|