...

View Full Version : validate html form (multiple selections)



Mr.Canuck
03-12-2012, 08:11 PM
Good day. This form is working, but I want to make the "What AIS services are you interested in?" multiple selection a "required field". The other required fields are working, but I can't figure out how to make this field "required".

<blockquote><h2>Request More Information:</h2>
<script type="text/javascript">
function MM_validateForm() { //v4.0
if (document.getElementById){
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
} else if (test!='R') { num = parseFloat(val);
if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
} if (errors) alert('The following error(s) occurred:\n'+errors);
document.MM_returnValue = (errors == '');
} }
</script>
<form action="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST" onsubmit="MM_validateForm('first_name','','R','last_name','','R','email','','RisEmail','company','','R','phone ','','RisNum');return document.MM_returnValue"><input type="hidden" name="oid" value="00Dd0000000cnUc" />
<input type="hidden" name="retURL" value="http://aissolutions.ca/thank-you" />
<p style="float:left;width:350px"><label for="first_name">First Name <span style="color:#f00">(required)</span></label><input id="first_name" type="text" name="first_name" size="50" maxlength="40" /></p>
<p style="float:left;width:350px"><label for="last_name">Last Name <span style="color:#f00">(required)</span></label><input id="last_name" type="text" name="last_name" size="50" maxlength="80" /></p>
<p style="float:left;width:350px"><label for="email">Email <span style="color:#f00">(required)</span></label><input id="email" type="text" name="email" size="50" maxlength="80" /></p>
<p style="float:left;width:350px"><label for="company">Company <span style="color:#f00">(required)</span></label><input id="company" type="text" name="company" size="50" maxlength="40" /></p>
<p style="float:left;width:350px"><label for="phone">Phone <span style="color:#f00">(required)</span></label><input id="phone" type="text" name="phone" size="50" maxlength="40" /></p>
<p style="float:left;width:350px">Best Time To Call:<select style="width:325px" id="00Nd0000004F2Mb" name="00Nd0000004F2Mb" title="Best Time To Call"><option value="">--None--</option><option value="9:00am to 12:00pm">9:00am to 12:00pm</option>
<option value="1:00pm to 5:00pm">1:00pm to 5:00pm</option>
<option value="6:00pm to 9:00pm">6:00pm to 9:00pm</option>
</select></p>
<p style="clear:left;float:left;width:350px"><label for="street">Address</label><br>
<textarea name="street" cols="47"></textarea></p>

<p style="float:left;width:350px">What AIS services are you interested in?:<select style="width:325px" id="00Nd00000043aHr" multiple="multiple" name="00Nd00000043aHr" title="What AIS services are you interested in?"><option value="Bookkeeping">Bookkeeping</option>
<option value="Business Incorporation">Business Incorporation</option>
<option value="Catch Up/Clean Up Records">Catch Up/Clean Up Records</option>
<option value="Job Costing">Job Costing</option>
<option value="Part Time CFO">Part Time CFO</option>
<option value="Software Conversion">Software Conversion</option>
<option value="Software Customization">Software Customization</option>
<option value="Software Training">Software Training</option>
<option value="Tax Services">Tax Services</option>
</select></p>

<p style="float:left;width:350px">Anything else you think we should know:<br>
<textarea id="00Nd0000004E2rs" name="00Nd0000004E2rs" cols="47"></textarea></p>

<p style="clear:left;float:left"><input type="submit" name="submit" /></p>

Thanks.

webdev1958
03-13-2012, 10:49 AM
The other required fields are working, but I can't figure out how to make this field "required"

You need to loop through the <select>'s options to check if at least one is selected.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title></title>
<style type="text/css"></style>
<script type="text/javascript">
function validateForm(){
var isDataValid = false;
var selO = document.getElementById('sel1');
for(i=0; i < selO.options.length; i++){
if(selO.options[i].selected){
isDataValid = true;
i=selO.options.length;
}
}
return isDataValid;
}
</script>
</head>
<body>
<form action="url1" method="post" onsubmit="return validateForm();">
<select id="sel1" multiple="multiple" name="sel1" >
<option value="Bookkeeping">Bookkeeping</option>
<option value="Business Incorporation">Business Incorporation</option>
<option value="Catch Up/Clean Up Records">Catch Up/Clean Up Records</option>
<option value="Job Costing">Job Costing</option>
<option value="Part Time CFO">Part Time CFO</option>
<option value="Software Conversion">Software Conversion</option>
<option value="Software Customization">Software Customization</option>
<option value="Software Training">Software Training</option>
<option value="Tax Services">Tax Services</option>
</select>
<input type="submit" value="Submit" />
</form>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum