SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2000
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a radiogroup designed like this:

    <input type="radio" name="paymenttype" value="visa" checked><font size="1">Visa</font><br>
    <input type="radio" name="paymenttype" value="discover"><font size="1">Discover</font><br>
    <input type="radio" name="paymenttype" value="mastercard"><font size="1">Mastercard</font><br>
    <input type="radio" name="paymenttype" value="check"><font size="1">Check</font><br>
    <input type="radio" name="paymenttype" value="amex"><font size="1">AMEX</font><br>
    <input type="radio" name="paymenttype" value="invoice"><font size="1">Invoice Me</font><br>
    <!-- The field credit is for the CC Number -->
    <input type="text" name="credit" size="19"><br>
    <!-- The field expiration is for the CC Expiration Date -->
    <input type="text" name="expiration" size="10"><font size="1">Expiration</font><br>

    Now in order to validate this group, I need to check to
    see if any of the CC types were selected, that is Visa, Discover,
    MasterCard, and AMEX. If they are selected, I need to check to see
    if a CC Number and Expire date were entered.

    I have some of it done from another validation script on another page.

    // These are the standard fields I need to validate...
    var neededFields = new makeAnArray('first_name', 'last_name', 'company',
    'address1', 'city', 'state', 'zip','country',
    'phone');

    // These are the error msg text for the fields above
    var neededDesc = new makeAnArray('Your First Name', 'Your Last name', 'Your Company Name',
    'Your Address', 'Your City', 'a State or Province',
    'a Postal or Zip code', 'a Country', 'a Phone Number');

    // These are the crdit card fields I need validated
    var neededccFields = new makeAnArray('paymenttype', 'credit', 'expiration');

    // And their error msg text...
    var neededccDesc = new makeAnArray('a Credit Card Type', 'a Credit Card Number', 'a Credit Card Expiration Date');

    function makeAnArray() {
    this.length = makeAnArray.arguments.length;
    for (var i = 0; this.length > i; i++) {
    this[i] = makeAnArray.arguments[i]
    }
    }

    // parameter obj is the FORM
    function validInfo(obj) {
    var errMsg = '';
    var fo = false;
    for (var i = 0; neededFields.length > i; i ++) {
    if ((obj.elements[neededFields[i]].value == '') | | (obj.elements[neededFields[i]].value == ' ')) {
    errMsg = errMsg + 'You didn\'t enter ' + neededDesc[i] + '\n';
    }
    }

    // Check the creditcard fields and return the err msg, if any...
    errMsg = errMsg + checkccfields(obj))

    if (errMsg != '') {
    errMsg = '[ Order Form Validation ]\nwas unable to process the form because:\n\n' + errMsg;
    alert(errMsg);
    return false;
    }
    else {
    return true;
    }
    }

    function checkccfields(obj) {
    var err = '';
    var radios;
    for (var i = 0; neededccFields.length > i; i ++) {
    // Kinda confused on what I need to do here...
    if (obj.elements[neededccFields[i]].type == 'radio') {
    radios = obj.elements[neededccFields[i]];
    }
    }
    else {
    // This will capture empty CC Number and Expire date...
    if ((obj.elements[neededccFields[i]].value == '') | | (obj.elements[neededccFields[i]].value == ' ')) {
    err = err + 'You didn\'t enter ' + neededccDesc[i] + '\n';
    }
    }
    }
    return err;
    }

    Any help on this would be greatly appreciated.

  2. #2
    Your Lord and Master, Foamy gold trophy Hierophant's Avatar
    Join Date
    Aug 1999
    Location
    Lancaster, Ca. USA
    Posts
    12,305
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You should validate the radio buttons separately after the main fields are validated. Then once you have a valid radio button you would call your function to validate credit card information.

    So your validation routine would flow like:

    checkNeededFields.
    checkRadio
    If Radio != Check and Radio != Invoice then
    checkcreditinfo

    For your radio checking you could say something as simple as:
    if obj.paymenttype.value != "check" && obj.paymenttype.value != "invoice" { checkccfields(obj) }

    Of course you then need to remove your radio button from the list of needed Credit Items as it shouldn't really be included in that group anyways.

    ------------------
    Wayne Luke

    The Majormud Encyclopedia - www.bloodquest2.com
    Come Play one of the hottest games on the net.



  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2000
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wrote onclick handlers for the radio buttons Check and Invoice to set boolean variables check and invoice to true, then re-wrote my checkccfields function like this:
    It seems to work fine...
    <BLOCKQUOTE><font size="1" face="Verdana, Arial">quote/font><HR>
    function checkccfields(obj) {
    var err = '';
    for (var i = 0; neededccFields.length &gt; i; i ++) {
    if ((!invoice) && (!check)) {
    if ((obj.elements[neededccFields[i]].value == '') &#0124; &#0124; (obj.elements[neededccFields[i]].value == ' ')) {
    err = err + 'You didn\'t enter ' + neededccDesc[i] + '\n';
    obj.elements[neededccFields[i]].focus();
    }
    }
    }
    return err;
    }
    <HR></BLOCKQUOTE>


Bookmarks

Posting Permissions

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