View Single Post
Old 10-03-2012, 03:11 PM   PM User | #1
rcacciato
New to the CF scene

 
Join Date: Oct 2012
Location: New York, NY USA
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
rcacciato is an unknown quantity at this point
Help with form validation

I have a form which contains 6 fields. Right now it validates a form submission to make sure each field was completed. I want to add a validation to check that whatever is entered in the "PromoCode" field contains the string "EASY" (the actual codes are something like EASY123, EASY 456, etc.). I can't figure out how to do this.

Here is the javascript:

<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>

Here is the form submit code:

<form action="ch_instant_rebate_q412_thankyou.php" method="post" name="form" id="form" onsubmit="MM_validateForm('First_Name','','R','Last_Name','','R','Company_Name','','R','Phone_Number ','','R','EMail_Address','','R', 'PromoCode','','R');return document.MM_returnValue">

The "ch_instant_rebate_q412_thankyou.php" page handles generating an email and displaying the thank you page.

How can I add the string validation to the PromoCode field? Any help would be appreciated. Thanks!
rcacciato is offline   Reply With Quote