By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,363 Members | 634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,363 IT Pros & Developers. It's quick & easy.

Mandatory Form Fields

P: 24
Hi I have set up a form. What I want to to is with the fields:

Company Postcode
Agency Number
Policy Number

I want these to be mandatory however if someone fills in the company postcode for example the other two fields dont need to be filled in. I just put a line of text in saying: Please ensure either Company Postcode, Agency Number or Policy Number is filled in. but this is not really working well as people are still leaving it blank and they need to able to have the option to fill either or but they need to be mandatory but not, if you know what I mean?

Code below:


[HTML]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>

<head>

<title>Proforma</title>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">

<script language="javascript">

function ABC_JScheckForm( f )
{

var sErrors = "";
var sfocusElements;
var aCheckFields = new Array();

aCheckFields[0] = "txt02_Name_of_Caller:::Name of Caller";
aCheckFields[1] = "txt03_Type_of_Caller:::Type of Caller";
aCheckFields[2] = "txt06_Tel_No:::Tel No";
aCheckFields[3] = "txt11_Comments:::Comments";


// Loop through each field identified as mandatory
for ( var i = 0; i < aCheckFields.length; i++ )
{
var aData = aCheckFields[i].split( /\:\:\:/ );
var oFormElement = eval( "f." + aData[0] );

if ( oFormElement.value.match( /^\s*$/ ) )
{
sErrors += aData[1] + "\n";

if ( ! sfocusElements ) { sfocusElements = aData[0]; }

}
}

if ( sErrors )
{
alert( "The following fields are required - \n\n" + sErrors );
eval( "f." + sfocusElements + ".focus()" );
return false;
}

return true;

}

</script>



<FORM name="frmSendMail" action="http://xxxxxxx/sendmail/" method="POST" onsubmit="return ABC_JScheckForm( this )">

<input type="hidden" name="ctrlSendToEmailAddress" value="Email@email.com=>Proforma"/>
<input type="hidden" name="ctrlEmailSubject" value="Proforma" />
<input type="hidden" name="ctrlMethod" value="E-MAIL" />
<input type="hidden" name="ctrlFromEmailAddress" value=""/>
<input type="hidden" name="ctrlRedirectURL" value="xxxx/xxxxx/submit/" />

<table width="90%" border="0" cellspacing="0" cellpadding="0" bgcolor="8ED1E6">
<tr>
<td valign="top" align="left">
<table width="100%" border="0" cellspacing="1" cellpadding="3" bgcolor="8ED1E6" align="center">
<tr valign="top" align="left">
<td class="tableBackLightestBlue" colspan="2" height="27">
<p class="pageHead">
<p style="color: #CC0000">Fields marked with * are mandatory</p>
</td>
</tr>


<!-- start of form details -->

<TR>
<TD class="tableBackLgtBlue2">EMAIL TYPE</td>
<td class="tableBackLgtBlue2"><select size="1" name="sel01_Email type">
<option value="Select">Select</option>
<option value="CALLBACK">CALLBACK</option>
<option value="INFO REQUEST">INFO REQUEST</option>
</select>
</td>
</tr>

<TR>
<TD class="tableBackLgtBlue2">NAME OF CALLER</td>
<td class="tableBackLgtBlue2" style="color: #CC0000"><input type="text" name="txt02_Name_of_Caller"> *</td>
</tr>

<TR>
<TD class="tableBackLgtBlue2">TYPE OF CALLER</td>
<td class="tableBackLgtBlue2" style="color: #CC0000"><input type="text" name="txt03_Type_of_Caller"> *</td>
</tr>

<TR>
<TD class="tableBackLightestBlue" colspan="2" height="27" style="color: #CC0000" align="center">Please ensure either <b>Company Postcode</b>, <b>Agency Number</b> or <b>Policy Number</b> is filled in.</TD>
</TR


<TR>
<TD class="tableBackLgtBlue2">COMPANY POSTCODE</td>
<td class="tableBackLgtBlue2"><input type="text" name="txt04_Company_Postcode"></td>
</tr>

<TR>
<TD class="tableBackLgtBlue2">AGENCY NUMBER</td>
<td class="tableBackLgtBlue2"><input type="text" name="txt05_Agency_Number"></td>
</tr>

<TR>
<TD class="tableBackLgtBlue2">TELEPHONE NUMBER</td>
<td class="tableBackLgtBlue2" style="color: #CC0000"><input type="text" name="txt06_Tel_No"> *</td>
</tr>

<TR>
<TD class="tableBackLgtBlue2">FAX NUMBER</td>
<td class="tableBackLgtBlue2"><input type="text" name="txt07_Fax_No"></td>
</tr>

<TR>
<TD class="tableBackLgtBlue2">POLICY NUMBER</td>
<td class="tableBackLgtBlue2"><input type="text" name="txt08_Policy_No"></td>
</tr>

<TR>
<TD class="tableBackLgtBlue2">HANDLER</td>
<td class="tableBackLgtBlue2"><input type="text" name="txt09_handler"></td>
</tr>

<TR>
<TD class="tableBackLgtBlue2">EMAIL ADDRESS</td>
<td class="tableBackLgtBlue2"><input type="text" name="txt10_Email_Address"></td>
</tr>

<tr>
<td class="tableBackLgtBlue2">COMMENTS</td>
<td class="tableBackLgtBlue2" style="color: #CC0000"><textarea name="txt11_Comments" cols="30" rows="4"></textarea> *</td>
</tr>

<TR>
<TD class="tableBackLgtBlue2">TURNAROUND TIMES</td>
<td class="tableBackLgtBlue2">Turn around time 24 Hours for all callbacks.

</td>
</tr>

<TR>
<TD class="tableBackLgtBlue2">EMAIL RECIPIENT</td>
<td class="tableBackLgtBlue2"><p>Email@email.com</p></td>
</tr>



<tr valign="top" align="left">
<td class="tableBackLgtBlue2" colspan="2">
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="19">
<tr valign="top" align="left">
<td width="20%">&nbsp;</td>
<td align="center">&nbsp; </td>
<td align="right" width="20%">
<INPUT TYPE="submit" class="abcButton" value="SEND MAIL" />
</td>
</tr>
</table>
</td>
</tr>

</table>
</td>

</tr>
</table>

<br><br>

</FORM>
</body>
</html>[/HTML]
Sep 20 '07 #1
Share this Question
Share on Google+
2 Replies

acoder
Expert Mod 15k+
P: 16,027
Moved from articles section. Do not post questions there.

Use code tags when posting code:
[CODE=html]
HTML code goes here.
[/code]
Sep 20 '07 #2

gits
Expert Mod 5K+
P: 5,390
hi ...

you would need to call a function on submit ... have a look at the following example:

Expand|Select|Wrap|Line Numbers
  1. function check_form() {
  2.     // give your fields to check an id (i use id1 ... id3 here)
  3.     // and put it in a map
  4.  
  5.     var field_map = { 'id1': 1, 'id2': 1, 'id3': 1 };
  6.     var val = false;
  7.  
  8.     for (var i in field_map) {
  9.         var field = document.getElementById(i);
  10.  
  11.         if (field.value != '') {
  12.             val = true;
  13.             break;
  14.         }
  15.     }
  16.  
  17.     return val;
  18. }
  19.  
this function returns true, when one of your fields has a value != '' ... otherwise it returns false.

kind regards
Sep 20 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.