Board index   FAQ   Search  
Register  Login
Board index php forum :: HTML JavaScript

Javascript form validation code not working properly in PHP

Javascript coding ..

Moderators: macek, egami, gesf

Javascript form validation code not working properly in PHP

Postby WRStrong » Fri Jun 11, 2004 4:19 pm

Hi guys! (and gals too, of course) :D

I have a javascript that validated a series of form fields.. like email address, telephone number, etc... and if everything was ok it would allow the submission of the form. This script worked perfectly in HTML... I have a link to an example script below..

http://www.baldeagleelectronics.com/contest2.html

When I try to modify it for use as a php page; however, it will not work at all. For example, if I were to fill in the form & leave the email address blank, it popped up a window telling you that the email address must be filled in...yadda, yadda. Now, it just goes right on ahead..

The php is below..(warning! It's rather long...)
<?
print("
<head>
<script language=\"JavaScript\">

function isEmailAddr(email)
{
var result = false;
var theStr = new String(email);
var index = theStr.indexOf(\"@\");
if (index > 0)
{
var pindex = theStr.indexOf(\".\",index);
if ((pindex > index+1) && (theStr.length > pindex+1))
result = true;
}

return result;
}

function validRequired(formField,fieldLabel)
{
var result = true;

if (formField.value == '')
{
alert('Please enter a value for the '\" + fieldLabel +\"' field.');
formField.focus();
result = false;
}

return result;
}

function validEmail(formField,fieldLabel,required)
{
var result = true;

if (required && !validRequired(formField,fieldLabel))
result = false;

if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
{
alert('Please enter a complete email address in the form: yourname@yourdomain.com');
formField.focus();
result = false;
}

return result;

}

function validNum(formField, fieldLabel)
{
var result = true;
var isn = /^\d+$/.test(formField.value);

if(!isn)
{
alert('Please enter a number for the '\" + fieldLabel +\"' field.');
formField.focus();
result = false;
}

return result;
}

function validPhone(formField, fieldLabel)
{
var result = true;

var phone_number = formField.value;
var pure_nums = phone_number.replace(/\D+/g, '');

if(pure_nums.length < 10)
{
formField.focus();
alert('The phone number must be no less than 10 digits, but no more than 11 digits. Please enter your telephone number.');
result = false;
}

if(pure_nums.length > 11)
{
formField.focus();
alert('The phone number must be no less than 10 digits, but no more than 11 digits. Please enter your telephone number.');
result = false;
}

if(!(/^(\d+|\-)+$/.test(phone_number)))
{
formField.focus();
alert('The phone number can contain only numbers and/or dashes. Please enter your telephone number.');
result = false;
}

return result;
}

function validateForm(theForm)
{
// Start ------->
if (!validRequired(theForm.Name,'Name'))
return false;

if (!validRequired(theForm.Address1,'Address1'))
return false;

if (!validRequired(theForm.City,'City'))
return false;

if (!validRequired(theForm.State,'State'))
return false;

if (!validNum(theForm.Zip,'Zip Code'))
return false;

if (!validEmail(theForm.email,'Email Address',true))
return false;

if (!validPhone(theForm.Telephone,'Telephone Number'))
return false;

// <--------- End

return true;
}
</script>

</head>
<BODY>
<center>
<TABLE border=0 style=\"background-color: #FFFFFF\">
<FORM ACTION=http://www.baldeagleelectronics.com/cgi-bin/formmail.pl METHOD=POST onsubmit=return validateForm(this)>
<INPUT TYPE=HIDDEN NAME=recipient VALUE=contests@baldeagleelectronics.com>
<INPUT TYPE=HIDDEN NAME=redirect VALUE=http://www.baldeagleelectronics.com/thanks.html>
<INPUT TYPE=HIDDEN NAME=subject VALUE=Contest_Entries>
<INPUT TYPE=HIDDEN NAME=env_report VALUE=HTTP_USER_AGENT,REMOTE_HOST,REMOTE_ADDR,HTTP_X_FORWARDED_FOR>
<TR>
<TD align=right colspan=2>
<p align=center><b><font size=3>Free Federal, State & Local Income Tax Returns!</font></b></TD>
</TR>
<TR>
<TD align=right colspan=2>&nbsp;</TD>
</TR>
<TR>
<TD align=right><b>Name:</b></TD>
<TD><B>
<input type=hidden name=contest value=Free_Federal,_State_&_Local_Income_Tax_Returns!>
<INPUT tabIndex=1 size=28 name=Name></B></TD>
</TR>
<TR>
<TD align=right><b>Address:
</b></TD>
<TD><B>
<INPUT tabIndex=2 size=28 name=Address1></B></TD>
</TR>
<TR>
<TD></TD>
<TD><B>
<INPUT tabIndex=3 size=28 name=Address2></B></TD>
</TR>
<TR>
<TD align=right><b>City:</b></TD>
<TD><B>
<INPUT tabIndex=4 size=28 name=City></B></TD>
</TR>
<TR>
<TD align=right><b>State:</b></TD>
<TD><B>
<SELECT tabIndex=5 size=1 name=State>
<OPTION value=\"\" selected></OPTION>
<OPTION value=Alabama>Alabama</OPTION>
<OPTION value=Alaska>Alaska</OPTION>
<OPTION value=Arizona>Arizona</OPTION>
<OPTION value=Arkansas>Arkansas</OPTION>
<OPTION value=California>California</OPTION>
<OPTION value=Colorado>Colorado</OPTION>
<OPTION value=Connecticut>Connecticut</OPTION>
<OPTION value=Delaware>Delaware</OPTION>
<OPTION value=District_of_Columbia>District of Columbia</OPTION>
<OPTION value=Florida>Florida</OPTION>
<OPTION value=Georgia>Georgia</OPTION>
<OPTION value=Hawaii>Hawaii</OPTION>
<OPTION value=Idaho>Idaho</OPTION>
<OPTION value=Illinois>Illinois</OPTION>
<OPTION value=Indiana>Indiana</OPTION>
<OPTION value=Iowa>Iowa</OPTION>
<OPTION value=Kansas>Kansas</OPTION>
<OPTION value=Kentucky>Kentucky</OPTION>
<OPTION value=Louisiana>Louisiana</OPTION>
<OPTION value=Maine>Maine</OPTION>
<OPTION value=Maryland>Maryland</OPTION>
<OPTION value=Massachusetts>Massachusetts</OPTION>
<OPTION value=Michigan>Michigan</OPTION>
<OPTION value=Minnesota>Minnesota</OPTION>
<OPTION value=Mississippi>Mississippi</OPTION>
<OPTION value=Misouri>Misouri</OPTION>
<OPTION value=Montana>Montana</OPTION>
<OPTION value=Nebraska>Nebraska</OPTION>
<OPTION value=Nevada>Nevada</OPTION>
<OPTION value=New_Hampshire>New Hampshire</OPTION>
<OPTION value=New_Jersey>New Jersey</OPTION>
<OPTION value=New_Mexico>New Mexico</OPTION>
<OPTION value=New_York>New York</OPTION>
<OPTION value=North_Carolina>North Carolina</OPTION>
<OPTION value=North_Dakota>North Dakota</OPTION>
<OPTION value=Ohio>Ohio</OPTION>
<OPTION value=Pennsylvania>Pennsylvania</OPTION>
<OPTION value=Rhode_Island>Rhode Island</OPTION>
<OPTION value=South_Carolina>South Carolina</OPTION>
<OPTION value=South_Dakota>South Dakota</OPTION>
<OPTION value=Tennessee>Tennessee</OPTION>
<OPTION value=Texas>Texas</OPTION>
<OPTION value=Utah>Utah</OPTION>
<OPTION value=Vermont>Vermont</OPTION>
<OPTION value=Washington>Washington</OPTION>
<OPTION value=West_Virginia>West Virginia</OPTION>
<OPTION value=Wisconsin>Wisconsin</OPTION>
<OPTION value=Wyoming>Wyoming</OPTION>
</SELECT></B></TD>
</TR>
<TR>
<TD align=right><B>Zip:</B></TD>
<TD><B>
<INPUT tabIndex=6 size=10 name=Zip></B></TD>
</TR>
<TR>
<TD></TD>
</TR>
<TR>
<TD align=right><b>E-Mail Address:</b></TD>
<TD><B>
<INPUT tabIndex=7 size=28 name=email></B></TD>
</TR>
<TR>
<TD></TD>
</TR>
<TR>
<TD align=right><b>Telephone #:</b></TD>
<TD><B>
<INPUT tabIndex=8 size=15 name=Telephone></B></TD>
</TR>
<TR>
<TD></TD>
</TR>
<TR>
<TD align=right colspan=2>
<center>
<input type=submit value=Submit id=Button>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type=reset name=Reset value=Reset id=Button>
</center>
</TD>
</TR>
<TR>
<TD align=right colspan=2>&nbsp;</TD>
</TR>
<TR>
<TD align=right colspan=2>
<p align=left>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Drawing will be held on December 31st.&nbsp; Winners will be notified via email.&nbsp; Winners will have three (3) calendar days to respond to winning notification.&nbsp; If winner does not respond, a new name will be drawn and the process will be repeated until there is a validated winner.</TD>
</TR>
<TR>
<TD align=right colspan=2>&nbsp;</TD>
</TR>
<TR>
<TD align=right colspan=2>
<center><font size= 1>We will <u><b>never</b></u> give out your personal information to anyone else without your consent.<br>Please view our <a target=_blank href=index.php?body=privacy>privacy statement</a> for further details.</font></center>
</form>
</table>
</center>
");
?>

Anybody have any suggestions???

Thanks for the help in advance,

B.
WRStrong
New php-forum User
New php-forum User
 
Posts: 24
Joined: Wed Mar 05, 2003 9:52 am
Location: Bellefonte, PA

Postby ruturajv » Sun Jun 13, 2004 8:12 pm

buddy please edit your post... truncate it to only what is required...

And I would recommend you using Mozilla Venkman Javascript Debugger

Its an Javascript IDE just like Visual Studio for VB !!! :-D
Cheers Mozilla
User avatar
ruturajv
php-forum Super User
php-forum Super User
 
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India


Return to JavaScript

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.