Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Validation 1

Status
Not open for further replies.

Tuck007

Technical User
Apr 29, 2002
105
US
Hi! I'm new to Javascript and wanted to make a small validation for my form. Here is what I have constructed thus far:
Code:
<script LANGUAGE=&quot;JAVASCRIPT&quot;>
<!-- hide script from older bowsers

function validateForm(form){
				 ret = true;
				 
				 if ( form.name.value == &quot;&quot; ){
				 		alert(&quot;A name is required.&quot;);
						ret = false;
				 }
				 if ( form.email.value == &quot;&quot; ){
				 		alert(&quot;An email address is required.&quot;);
						ret = false;
				 }
				 return( ret);
}

//  end of code hiding -->
</script>
<form action=&quot;send.php&quot; method=&quot;post&quot; name=&quot;sendMassage&quot;>
<input type=&quot;hidden&quot; name=&quot;date&quot; value=&quot;<?php $now = time(); localtime(time(),true); getdate(); print date(&quot;m/d/y, g:i:s A&quot;); ?>&quot;>
<input type=&quot;hidden&quot; name=&quot;ip&quot; value=&quot;<?php print $HTTP_SERVER_VARS['REMOTE_ADDR']; ?>&quot;>
<tr>
<th align=&quot;right&quot;>Name :</th><th colspan=&quot;3&quot; align=&quot;left&quot;><input type=&quot;text&quot; name=&quot;name&quot;></th>
</tr>
<tr>
<th align=&quot;right&quot;>Email :</th><th colspan=&quot;3&quot; align=&quot;left&quot;><input type=&quot;text&quot; name=&quot;name&quot;></th>
</tr>
<tr>
<th align=&quot;right&quot;>Phone :</th><th colspan=&quot;3&quot; align=&quot;left&quot;><input type=&quot;text&quot; name=&quot;name&quot;></th>
</tr>
<tr>
<th valign=&quot;top&quot; align=&quot;right&quot;>Message :</th><th colspan=&quot;3&quot; align=&quot;left&quot;><textarea name=&quot;message&quot; rows=&quot;7&quot; cols=&quot;45&quot;></textarea></th>
</tr>
<tr>
<td colspan=&quot;4&quot; align=&quot;center&quot;><input class=&quot;colorbtn&quot; type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Send&quot; onSubmit=&quot;validateForm(this.form)&quot;> <input class=&quot;colorbtn&quot; type=&quot;reset&quot; value=&quot;Clear&quot;></th>
</tr>
</form>

When the submit button is pressed, the form is still sent, but there are blank feilds for name and email. Suggestions? .:TUCK:.
 
well, first you are making things more difficult then they need to be by passing the form parameter. seeing as you a directly referncing the form values then you don't need to do this. Also you need to name the form fields accordingly.
next the onSubmit goes in the form tag. hmm.. I think that's it. I also added the return statement in the onSubmit so it works in NN and changed the return in the function slightly. take a look and see how this is. hope it helps

<script LANGUAGE=&quot;JAVASCRIPT&quot;>
<!-- hide script from older bowsers

function validateForm(){
if ( sendMassage.name.value == &quot;&quot; ){
alert(&quot;A name is required.&quot;);
return false;
}
if ( sendMassage.email.value == &quot;&quot; ){
alert(&quot;An email address is required.&quot;);
return false;
} else {
return true;
}
}

// end of code hiding -->
</script>
<form action=&quot;send.php&quot; method=&quot;post&quot; name=&quot;sendMassage&quot; onSubmit=&quot;return validateForm()&quot;>
<input type=&quot;hidden&quot; name=&quot;date&quot; value=&quot;<?php $now = time(); localtime(time(),true); getdate(); print date(&quot;m/d/y, g:i:s A&quot;); ?>&quot;>
<input type=&quot;hidden&quot; name=&quot;ip&quot; value=&quot;<?php print $http_SERVER_VARS['REMOTE_ADDR']; ?>&quot;>
<tr>
<th align=&quot;right&quot;>Name :</th><th colspan=&quot;3&quot; align=&quot;left&quot;><input type=&quot;text&quot; name=&quot;name&quot;></th>
</tr>
<tr>
<th align=&quot;right&quot;>Email :</th><th colspan=&quot;3&quot; align=&quot;left&quot;><input type=&quot;text&quot; name=&quot;email&quot;></th>
</tr>
<tr>
<th align=&quot;right&quot;>Phone :</th><th colspan=&quot;3&quot; align=&quot;left&quot;><input type=&quot;text&quot; name=&quot;phone&quot;></th>
</tr>
<tr>
<th valign=&quot;top&quot; align=&quot;right&quot;>Message :</th><th colspan=&quot;3&quot; align=&quot;left&quot;><textarea name=&quot;message&quot; rows=&quot;7&quot; cols=&quot;45&quot;></textarea></th>
</tr>
<tr>
<td colspan=&quot;4&quot; align=&quot;center&quot;><input class=&quot;colorbtn&quot; type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Send&quot;> <input class=&quot;colorbtn&quot; type=&quot;reset&quot; value=&quot;Clear&quot;></th>
</tr>
</form> I dare to learn more
admin@onpntwebdesigns.com
 
I see! If I not named the form, then I could refer to specific feilds like I had: form.name.value, right? Having the onSubmit in the FORM tag will send the form values through the validation when the submit button is pressed and then submit the form. That makes a lot more sense. Thanks! .:TUCK:.
 
Also it is generally a bad idea to name your fields words like &quot;name&quot; that could be misinterpreted by the javascript to mean the name attribute of the form. You may want to rename this something like &quot;txtName&quot;

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top