Validation script conditional on check box

Discussion in 'JavaScript' started by dtimo, Aug 18, 2009.

  1. #1
    I have a validated form for entering credit card details. However, some users wish to give these details over the phone.
    I wish to include a check box (document.form1.ccphone) for people who choose this option. When checked (=phone) this disables the javascript validation.

    I do not know javascript at all and this is a rare case where I need to use it. So I could REALLY use some help on how to write this conditional statement.

    Here is the validation code (Yeah, I know--it's scorge code from Dreamweaver):

    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);
    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 == '');
    } }

    I thank you for your help.

     
    dtimo, Aug 18, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    First thing you'll want to do if you expect anyone to help you here is to beautify that code. There's online sites that do this, but really, you, the thread starter need to put in the effort.

    Also add (code) and (/code) (replace with []) around the code.
     
    premiumscripts, Aug 18, 2009 IP
  3. prasanthmj

    prasanthmj Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    45
    #3
    Try this javascript form validation script here: http://www.javascript-coder.com/html-form/javascript-form-validation.phtml

    It supports 'conditional' validations (validate only if a check box is checked)
    example:
    
    form1Validator.addValidation("ccnum","req","Please fill in credit car number","VWZ_IsChecked(document.form1.elements['cccheck'],'')");
    
    Code (markup):
     
    prasanthmj, Aug 19, 2009 IP