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 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Posts
    436
    Thanks
    1
    Thanked 0 Times in 0 Posts

    complex validity check script

    When someone fills the contact form in my website - he has three options to be contacted by: phone number, cellphone number and email.

    The user should have the option to fill in one of the options or more (one or two or three, but not none).

    The script now runs validity check on all fields (on email, phonenumber AND cellnumber).
    I need to change the script so it'll allow the user to fill only two fields (or one, or three - whatever he chooses) and then will run the validity check only on the ones he filled.

    *a filled field is a field with more than 0 chars in it.

    Thanks for any help!

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <form onsubmit="for (i=0; i<this.elements('contacts[]').length; i++ ) if ( this.elements('contacts[]')[i].value.length != 0 ) chkValidity(this.elements('contacts[]')[i]); ">
    	Phone Number: <input type="Text" name="contacts[]"><br />
    	Cell Phone:   <input type="Text" name="contacts[]"><br />
    	Email:		  <input type="Text" name="contacts[]"><br />
    	<input type="Submit">
    </form>
    Note:I assume that your validity check function is chkValidity().

  • #3
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    this.elements('contacts[]')[i]

    should really be

    this.elements['contacts[]'][i]
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Posts
    436
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Originally posted by ConfusedOfLife
    Code:
    <form onsubmit="for (i=0; i<this.elements('contacts[]').length; i++ ) if ( this.elements('contacts[]')[i].value.length != 0 ) chkValidity(this.elements('contacts[]')[i]); ">
    	Phone Number: <input type="Text" name="contacts[]"><br />
    	Cell Phone:   <input type="Text" name="contacts[]"><br />
    	Email:		  <input type="Text" name="contacts[]"><br />
    	<input type="Submit">
    </form>
    Note:I assume that your validity check function is chkValidity().
    I don't understand - how come all three fields has the same name?!

  • #5
    Regular Coder
    Join Date
    Nov 2002
    Posts
    596
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>untitled</title>
    <script type="text/javascript" language="javascript">
    
    function checkform(oForm) {
         var el, none = true, re = /^\s*$/, msg = '';
         el = oForm.elements['phonenumber'];
         if (!re.test(el.value)) {
             none = false;
             if (!chkPhone(oForm)) return false;
       }
         el = oForm.elements['cellnumber'];
         if (!re.test(el.value)) {
             none = false;
             if (!chkCell(oForm)) return false;
       }
         el = oForm.elements['email'];
         if (!re.test(el.value)) {
             none = false;
             if (!chkEmail(oForm)) return false;
       }
         if (none) {
             msg += 'Please enter one of the following so that we may contact you:\n\n';
             msg += '\t___your PHONE NUMBER\n';
             msg += '\t___your CELL PHONE NUMBER\n';
             msg += '\t___your E-MAIL ADDRESS\n\n';
             msg += 'God bless you, my child.';
             alert(msg);
             oForm['phonenumber'].focus();
             oForm['phonenumber'].select();
             return false;
       }
         return true;
    }
    
    function chkPhone(oForm) {
         var field = oForm.phonenumber;
         alert(field.value);
         //validation here
         return true;
    }
    
    function chkCell(oForm) {
         var field = oForm.cellnumber;
         alert(field.value);
         //validation here
         return true;
    }
    
    function chkEmail(oForm) {
         var field = oForm.email;
         alert(field.value);
         //validation here
         return true;
    }
    
    </script>
    </head>
    <body>
    <form action="javascript&#58;alert('ok')" onsubmit="return checkform(this)">
    <strong>Please contact me by:</strong><br />[fill in at least one]<br /><br />
    <input type="text" name="phonenumber">____phone_number<br />
    <input type="text" name="cellnumber">____cell_phone_number<br />
    <input type="text" name="email">____email_address<br /><br />
    <input type="submit" value="DONE">
    </form>
    </body>
    </html>
    Last edited by cheesebagpipe; 04-11-2003 at 01:11 AM.

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by ShMiL
    I don't understand - how come all three fields has the same name?!
    I really don't have anything to do with the names, I use the elements array and I iterate it getting their values. It means even if you change the names to anything else it works! But if you wana know why, it's because when you put a name for a form element, when it's submited, you'll get it back in the server side language with the same name. In case of PHP ( a server side language ) it also gives you the option of defining an array name for the elements that you like, then when you get your inputs in PHP, you will have an array to work with. You don't have to remember any name. It's really no need that you do this if you're working client side.

  • #7
    Regular Coder
    Join Date
    Jul 2002
    Posts
    436
    Thanks
    1
    Thanked 0 Times in 0 Posts
    cheesebagpipe - your solution can be very helpful.
    Is there a way to run on every one different validity check?
    In your example - it checks if one of the fields is filled - is there a way to check if one of those fields:
    email
    phone_num
    cell_num

    passes those check-functions:
    chkEmail()
    chkPhone()
    chkCell()
    (chkEmail checks email, chkPhone checks phone_num, chkCell checks cell_num)

    thanks alot!

  • #8
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    fValidate does that

    <input type="text" name="phonenumber" alt="phone|bok">____phone_number<br />
    <input type="text" name="cellnumber" alt="phone|bok">____cell_phone_number<br />
    <input type="text" name="email" alt="email|bok">____email_address<br /><br />
    <input type="hidden" alt="atleast|1|,|phonenumber,cellnumber,email" />

    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #9
    Regular Coder
    Join Date
    Nov 2002
    Posts
    596
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ShMiL

    Edited the above; hope it's OK.
    Assume you've got those check-functions...


  •  

    Posting Permissions

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