Current location: Hot Scripts Forums » General Web Coding » JavaScript » Form validation - required fields


Form validation - required fields

Reply
  #1 (permalink)  
Old 08-12-04, 09:04 AM
Funky_Monk Funky_Monk is offline
New Member
 
Join Date: Jun 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Form validation - required fields

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 08-16-04, 11:26 AM
Chris Hill Chris Hill is offline
Newbie Coder
 
Join Date: Mar 2004
Location: UK
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Basically you have forgotten to give most of the options a value, so they are called "Country Name" but have the value of "" (nothing). In order to fix it, you are going to have to give each country a value.

Code:
<select size="1" name="Country" style="width:185px">
<option selected value="">--- SELECT COUNTRY ---</option>
<option value="Afghanistan">Afghanistan</option>
<option value="Albania">Albania</option>
...
...
...
<option value="Zimbabwe">Zimbabwe</option>
</select>
Also, you have given the drop down menu 2 id's, only one is needed, the "name", or the "id".

Chris
__________________
<a href="http://www.WhiteHillStudios.co.uk/home.php" target="_blank">White Hill Studios</a> - driving down the cost of web design<br>
<a href="http://www.agscriptz.com" target="_blank">AG Scriptz ii: renaissance</a> - a new dawn or scripting
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
lock form fields using checkbox jonathen JavaScript 5 02-04-05 04:35 PM
formmail problem gscraper Perl 12 08-27-04 04:06 AM
Disable form fields to be submitted RickyRod JavaScript 2 05-24-04 11:15 AM
displaying fields on the same form aliasgar ASP 1 01-22-04 01:50 PM
searching from a form where some fields are not required bobdole PHP 1 12-08-03 05:14 AM


All times are GMT -5. The time now is 12:34 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.