...

View Full Version : Form validation - required fields



Funky Monk
08-12-2004, 01:58 PM
Hi. I've got a form that I want to validate so that all fields are required. The code for the page is very long so I've reproduced snippets below:

This is in the Head section:

<script language="javascript" type="text/javascript">

<!-- hide from older browsers -->

function validForm(Form) {
if (Form.email.value =="")
{
alert("You must enter an e-mail address")
Form.email.focus()
return false
}
if (Form.email.value !=Form.email2.value)
{
alert("Entered e-mail addresses did not match")
Form.email.focus()
Form.email.select()
return false
}

if (Form.firstname.value =="")
{
alert("Please tell us your First Name")
Form.firstname.focus()
Form.firstname.select()
return false
}
if (Form.lastname.value =="")
{
alert("Please tell us your Last Name")
Form.lastname.focus()
Form.lastname.select()
return false
}
countryChoice = Form.Country.selectedIndex
if (Form.Country.options[countryChoice].value == "")
{
alert("Please tell us where you live")
return false
}
return true
}

// end hiding script -->
//-->
</script>

The form I am using is:

<form onSubmit="return validForm(this)" action="<?php echo $editFormAction; ?>" method="POST" name="frmSubscribe" id="frmSubscribe">
<p> <span class="sub-heading-1">Sign up for Mailing List:</span><br />
</p>
<table width="444" border="0" align="center">
<tr>
<td width="150" class="sub-heading">E-mail Address:</td>
<td width="240"><input type="text" name="email" size="40"/>
</td>
</tr>
<tr>
<td class="sub-heading">Confirm e-mail address:</td>
<td><label>
<input name="email2" type="text" id="email2" size="40">
</label></td>
</tr>
<tr>
<td class="sub-heading">First Name:</td>
<td><input name="firstname" type="text" /></td>
</tr>
<tr>
<td class="sub-heading">Last Name:</td>
<td><input type="text" name="lastname" /></td>
</tr>
<tr>
<td class="sub-heading">Country:</td>
<td><select name="Country" id="Country" style="width:185px" >
<option value= "" selected="selected">-- choose your Country --</option>
<option value="Afghanistan">Afghanistan</option>
<option>Albania</option>
-- too many countries to post here --
<option>Yugoslavia</option>
<option>Zambia</option>
<option>Zimbabwe</option>
</select></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><input name="Subscribe" type="submit" id="Subscribe"
value="Subscribe" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>


<input type="hidden" name="MM_insert" value="frmSubscribe">
</form>

What I am getting is that the Alert box works fine prompting the user to enter the required info until you get to the Country field. If you leave it blank it gives the alert and when you choose a country it still gives the alert. It seems to have got stuck and won't let me submit the form at all.

Willy Duitt
08-12-2004, 02:09 PM
I only see that your first two options actually have values being declared....

<option value= "" selected="selected">-- choose your Country --</option>
<option value="Afghanistan">Afghanistan</option>
<option>Albania</option>

In the above example, Albania has no value so of course the value will remain empty and your validation function will fail...

.....Willy

Willy Duitt
08-12-2004, 02:17 PM
Aw heck....
Your question was already answered here (http://www.webdeveloper.com/forum/showthread.php?s=&threadid=41613)....

Have a nice day;
.....Willy

jamescover
08-12-2004, 04:08 PM
08-12-2004 04:58 AM: I only see that your first two options actually have values being declared....




08-12-2004 02:16 PM: Thanks, but I seem to have fixed it myself.



:eek:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum