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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript for required fields

Status
Not open for further replies.

TheEnigma

Technical User
May 26, 2002
69
US
Hi,

I need help please. I have created an online form that I need with. I need to ensure that a couple of fields are filled in. If they aren't then a message pops up telling them to fill in these fields or they won't be able to proceed. Can anyone help me please as I don't know anything about javascript.

I have pasted the code that I have used for the form (minus the fields I don't need validated to make this smaller) below.



<head>
<title>New Enquiry</title>
<meta http-equiv=&quot;Page-Enter&quot; content=&quot;blendTrans(Duration=1.0)&quot;>
</head>

<body bgcolor=&quot;35359A&quot;>
<FORM ACTION=&quot; METHOD=&quot;POST&quot;>
<p align=&quot;center&quot;>&nbsp;</p>
<table width=&quot;70%&quot; border=&quot;2&quot; align=&quot;center&quot; cellpadding=&quot;3&quot; cellspacing=&quot;0&quot; bordercolor=&quot;#FFFFFF&quot;>
<tr>
<td colspan=&quot;2&quot;><div align=&quot;center&quot;><font color=&quot;#FFFF00&quot; size=&quot;2&quot; face=&quot;Verdana, Arial, Helvetica, sans-serif&quot;>* Indicates a
required field</font></div></td>
</tr>
<tr>
<td width=&quot;42%&quot;><font color=&quot;#FFFFFF&quot; size=&quot;2&quot; face=&quot;Verdana, Arial, Helvetica, sans-serif&quot;>First
Name</font></td>
<td width=&quot;58%&quot;> <INPUT NAME=&quot;First Name&quot; TYPE=&quot;text&quot; id=&quot;First Name&quot; SIZE=16 ></td>
</tr>
<tr>
<td><font color=&quot;#FFFFFF&quot; size=&quot;2&quot; face=&quot;Verdana, Arial, Helvetica, sans-serif&quot;>Last
Name </font></td>
<td><INPUT NAME=&quot;Surname&quot; TYPE=&quot;text&quot; id=&quot;Surname&quot; SIZE=16 ></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><font color=&quot;#FFFFFF&quot; size=&quot;2&quot; face=&quot;Verdana, Arial, Helvetica, sans-serif&quot;>State</font></td>
<td><font face=&quot;Verdana&quot; color=&quot;#000000&quot;><b><font size=&quot;1&quot; face=&quot;Arial, Helvetica, sans-serif&quot; color=&quot;#FFFFFF&quot;>
<select name=State size=1 id=&quot;State&quot;
style=&quot;COLOR: #000080; FONT-FAMILY: Verdana&quot;>
<option>.. Select A State ..</option>
<option value=&quot;ACT&quot;>Australian Capital Territory</option>
<option value=&quot;NSW&quot;>New South Wales</option>
<option value=&quot;NT&quot;>Northern Territory</option>
<option value=&quot;QLD&quot;>Queensland</option>
<option value=&quot;SA&quot;>South Australia</option>
<option value=&quot;TAS&quot;>Tasmania</option>
<option value=&quot;VIC&quot;>Victoria</option>
<option value=&quot;WA&quot;>Western Australia</option>
</select>
</font></b></font></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><font face=&quot;Verdana&quot; color=&quot;#000000&quot;><b><font size=&quot;1&quot; face=&quot;Arial, Helvetica, sans-serif&quot; color=&quot;#FFFFFF&quot;>
</font></b></font></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><font face=&quot;Verdana&quot; color=&quot;#000000&quot;><b><font face=&quot;Verdana&quot; color=&quot;#000000&quot;><b><font size=&quot;1&quot; face=&quot;Arial, Helvetica, sans-serif&quot; color=&quot;#FFFFFF&quot;>
</font></b></font></b></font></td>
</tr>
<tr>
<td><font color=&quot;#FFFFFF&quot; size=&quot;2&quot; face=&quot;Verdana, Arial, Helvetica, sans-serif&quot;>Total
Est Debt: <font size=&quot;1&quot;>(excluding mortgage)</font></font></td>
<td><font face=&quot;Arial, Helvetica, sans-serif&quot; color=&quot;#FFFFFF&quot; size=&quot;1&quot;>
<select name=estimated-debt size=1 id=&quot;estimated-debt&quot;
style=&quot;COLOR: #000080; FONT-FAMILY: Verdana&quot;>
<option>Estimated Debt</option>
<option value=&quot;$4,000&quot;>$4,000</option>
<option value=&quot;$5,000&quot;>$5,000</option>
<option value=&quot;$6,000&quot;>$6,000 </option>
<option value=&quot;$7,000&quot;>$7,000 </option>
<option value=&quot;$8,000&quot;>$8,000 </option>
<option value=&quot;$9,000&quot;>$9,000 </option>
<option value=&quot;$10,000&quot;>$10,000 </option>
<option value=&quot;Over $10,000&quot;>Over $10,000 </option>
</select>
</font></td>
</tr>
<tr>
<td><font color=&quot;#FFFFFF&quot; size=&quot;2&quot; face=&quot;Verdana, Arial, Helvetica, sans-serif&quot;>Net
Weekly Income:</font></td>
<td><font face=&quot;Arial, Helvetica, sans-serif&quot; color=&quot;#FFFFFF&quot; size=&quot;1&quot;>
<select name=NetIncome size=1 id=&quot;NetIncome&quot;
style=&quot;COLOR: #000080; FONT-FAMILY: Verdana&quot;>
<option>Net Income</option>
<option value=&quot;Less than $627 p/w&quot;>Less than $627 p/w</option>
<option value=&quot;Over $627 p/w&quot;>Over $627 p/w</option>
</select>
</font></td>
</tr>
<tr>
<td><font color=&quot;#FFFF33&quot; size=&quot;2&quot; face=&quot;Verdana, Arial, Helvetica, sans-serif&quot;>*</font><font color=&quot;#FFFFFF&quot; size=&quot;2&quot; face=&quot;Verdana, Arial, Helvetica, sans-serif&quot;>E-mail
address </font></td>
<td>
<input name=&quot;txtFrom&quot; type=&quot;text&quot; id=&quot;txtFrom&quot; size=31 maxlength=&quot;50&quot;></td>
</tr>
</table>
<p>&nbsp;</p>
<p align=&quot;center&quot;>&nbsp; </p>
<p align=&quot;center&quot;>
<INPUT TYPE=&quot;submit&quot; VALUE=&quot;Send message&quot;>
<font color=&quot;#FFFFFF&quot; size=&quot;2&quot; face=&quot;Verdana, Arial, Helvetica, sans-serif&quot;>or</font>
<INPUT TYPE=&quot;reset&quot; VALUE=&quot;Reset Form&quot;>
<INPUT TYPE=&quot;hidden&quot; NAME=&quot;txtSubject&quot; VALUE=&quot;New Enquiry&quot;>
<INPUT TYPE=&quot;hidden&quot; NAME=&quot;txtTo&quot; VALUE=&quot;enquiries@foxsymes.com.au&quot;>
<INPUT TYPE=&quot;hidden&quot; NAME=&quot;Redirect&quot; VALUE=&quot;</FORM>
 
here is the down to basic example of checking for a empty field
in the form tag do
<form name=&quot;frm&quot; onSubmit=&quot;return val()&quot;>

<scriptlanguage=&quot;javascript&quot;>
function val() {
if (frm.email.value == &quot;&quot;) {
alert(&quot;please fill in the email field!&quot;);
frm.email.focus();
return false;
}
else
{
return true;
}
}
</script>

REAL BASIC and I must say long winded if you do this for every field but it will get you what you want for this form.

you can go into looping thru all the form elements and such other functions to speed things up etc.. but if your new to this kind of coding then start out this basic and move on after you have things up and running.

if you would like to learn more there are many FAQ's here at Tek-Tips on form validation and there are a endless amount of open source scripts out online to search thru.

Hope that helps out A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
Thank you onpnt. This is he type of help I do need. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top