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 11 of 11
  1. #1
    New Coder
    Join Date
    Sep 2004
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Form validation headache

    Hi, as you can see below on my page I have a checkbox and text input box, there are a number of these on the page. The checkbox is always "orderxxx" where xxx is a number while the text input box is always "orderxxx_q", an example being:

    <input name="order223" type="checkbox" value="MailTuff">
    <input name="order223_q" type="text">

    Is there any way when the user presses submit that the form wont submit unless both have been checked/entered? Bear in mind that there are a few of these on the page and if the user doesnt want to order something he/she will simply leave both blank. I'm just looking to validate if he/she checks the box but doesnt enter a value or enters a value but doesnt check the box.

    Please help!

  • #2
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just replace the alert with doc.submit();



    Code:
    <script type="text/javascript">
    <!--
    
    function chkFrm(){
    
    var doc = document['oFrm'];
    for (var i=0;i<doc.length;i++){
    if (doc[i].type == "text" && doc[i].value.length > 0 && doc[i-1].checked){
    alert (doc[i].name + "\n" + doc[i-1].name);
    
    }
    }
    }
    
    //-->
    </script>
    
    
    
    
    <form name="oFrm" action="someFile.php">
    <input name="order223" type="checkbox" />
    <input name="order223_q" type="text" />
    <input name="order224" type="checkbox" />
    <input name="order224_q" type="text" />
    <input name="oBtn" type="button" value="submit" onclick="javascript:chkFrm();" />
    </form>
    Last edited by jamescover; 09-01-2004 at 04:17 PM.
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #3
    New Coder
    Join Date
    Sep 2004
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts
    on the form is a list of items, what if someone doesnt want to order a certain item, will this script not just alert them that the havent filled out the form? i just want it if they enter a quantity but not tick the box or they tick the box but dont enter a quantity? do you know what i mean?

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    jamescover

    Won't the form be submitted each time the statement is true?

    Or on the first occurrence
    Last edited by Mr J; 09-01-2004 at 05:42 PM.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #5
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I would suggest using a regular expression to match and pair the order223 input with its sister input which included the _q and validate by these pairs while looping thru the form elements....

    But you will most likely recieve better help if you provide some form code to work with... I would have tried to put together a quick script but I do not have time to script an entire form to write and test apprx. 12 lines of javascript....

    ....Willy

  • #6
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Won't the form be submitted each time the statement is true?
    Or on the first occurrence
    Absolutely! Isn't that what denhamd2 asked for?:



    there are a few of these on the page and if the user doesnt want to order something he/she will simply leave both blank. I'm just looking to validate if he/she checks the box but doesnt enter a value or enters a value but doesnt check the box
    Again:


    i just want it if they enter a quantity but not tick the box or they tick the box but dont enter a quantity? do you know what i mean?


    -james
    Last edited by jamescover; 09-02-2004 at 03:55 AM.
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #7
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Sorry, just trying to understand your code.

    The way I see it if checkbox "order223" is checked and textbox "order223_q" has text the form submits even though checkbox "order224" is checked and textbox "order224_q" is empty

    unless as you say I am misunderstanding the question
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #8
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The way I see it if checkbox "order223" is checked and textbox "order223_q" has text the form submits even though checkbox "order224" is checked and textbox "order224_q" is empty

    Right, right. I see. Sorry, Mr J, took me a while to catch on...



    You'll need some type of error checking to make sure at least one pair is filled out, but the following should take care of the first problem (don't have time to finish it right now...maybe, tonight):

    Code:
    <script type="text/javascript">
    <!--
    
    function chkFrm(){
    
    var doc = document['oFrm'];
    for (var i=0;i<doc.length;i++){
    if (doc[i].type == "text" && doc[i].value.length > 0 && !doc[i-1].checked){
    alert("please, check the appropriate box");
    return false;
    }
    
    if (doc[i].checked && doc[i+1].value == ""){
    alert ("please, type in your order");
    return false;
    }
    }
    doc.submit();
    }
    //-->
    </script>
    
    
    
    <form name="oFrm" action="someFile.php">
    <input name="order223" type="checkbox" />
    <input name="order223_q" type="text" />
    <input name="order224" type="checkbox" />
    <input name="order224_q" type="text" />
    <input name="order225" type="checkbox" />
    <input name="order225_q" type="text" />
    <input name="oBtn" type="button" value="submit" onclick="javascript:chkFrm();" />
    </form>

    -james
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #9
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jamescover
    (don't have time to finish it right now...maybe, tonight):
    Well , the O/P as not returned so I wouldn't worry about except for personal satisfaction...

    .....Willy

  • #10
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well , the O/P as not returned so I wouldn't worry about except for personal satisfaction...

    I'm with you...


    will this script not just alert them that the havent filled out the form?
    In fact, the above statement is why I didn't fix the script the first time around...he/she apparently hadn't even tested it.

    But I wanted to respond to MrJ...who pointed out my oversight.


    Good advice. I'll wait and see...


    -james
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #11
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.


  •  

    Posting Permissions

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