Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
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
Old 10-03-2012, 03:41 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 945
Thanks: 7
Thanked 97 Times in 97 Posts
WolfShade is an unknown quantity at this point
You can use RegEx for that. If the code will always begin with EASY, use the following as the mask:

/^EASY/
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 10-03-2012, 04:20 PM   PM User | #3
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
Thanks WolfShade. How do I write the validation statement? I'm a javascript newbie...
rcacciato is offline   Reply With Quote
Old 10-03-2012, 05:24 PM   PM User | #4
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 945
Thanks: 7
Thanked 97 Times in 97 Posts
WolfShade is an unknown quantity at this point
Code:
patt = /^EASY/;
if(patt.test(string)){
  // code for true
}
else {
 // code for false
}
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:36 AM.


Advertisement
Log in to turn off these ads.