Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Location
    New York, NY USA
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    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".

  • #3
    New to the CF scene
    Join Date
    Oct 2012
    Location
    New York, NY USA
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks WolfShade. How do I write the validation statement? I'm a javascript newbie...

  • #4
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    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".


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •