form requirement and halting issue

Discussion in 'JavaScript' started by stauf, Jun 19, 2007.

  1. #1
    Hi, I'm one of todays new guys here for JS and PHP. I don't know either that well but I'm tryin to learn. I used to work with Delphi so I know flow and such.

    My issue. I have a form that gets processed to a php page as an order form. What I need is a way to require one name field be filled in, and unless it is, the form will not continue to parse to the php page. Basically I need to trap the user until he/she provides their name. What I found/have now seems to know that the field is blank, but it doesn't trap the user, it simply continues to get parsed.

    
    <script language="JavaScript" type="text/javascript">
    <!-- Begin
    function verify() {
    var themessage = "You are required to complete the following fields: ";
    if (document.order.nick.value=="") {
    themessage = themessage + " -  Handle";
    }
    //alert if fields are empty and cancel form submit
    if (themessage == "You are required to complete the following fields: ") {
    document.order.submit();
    }
    else {
    alert(themessage);
    return false;
       }
    }
    //  End -->
    </script>
    
    
    // Then in the form
    
    <form action="order.php" method="post" name="order" onSubmit="verify();">
    
    // I've also tried the OnClick handler on the Sumbit buton, which yeilds the same results.
    
    Code (markup):
    Any suggestions? Thanks!
     
    stauf, Jun 19, 2007 IP
  2. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use "return" with onsubmit, and do not use .submit()

    <form action="order.php" method="post" name="order" onSubmit="[B]return [/B]verify([B]this[/B]);">
    Code (markup):
    function verify(nForm){
    
         if (nForm['nick'].value == "")
                    {
                     alert('Please complete the name field');
                     return false;
                    }
         return true;
    }
    Code (markup):
     
    Mike H., Jun 19, 2007 IP
  3. stauf

    stauf Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank You Mike!

    I can't believe how simple you made it. And how similar it looks to 'back in the day with delphi'...

    I hate getting old, I've forgotten more then I care to remember.. lol

    Thanks again!
     
    stauf, Jun 19, 2007 IP
  4. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You're welcome. Talk about old, I'll be 52 in September. And yes, I don't like it one bit.
     
    Mike H., Jun 19, 2007 IP
  5. stauf

    stauf Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Aw 52 ain't old.. If you said you were 70, then I'd say you were gettin there.. I'm not there yet, but if I'm this bad currently, I dunno if I wanna know about the future.. lol

    Now if I may, one question. How/Where would I add additional fields to be checked? Is there an OR operand that can be used like: ?

    if (nForm['nick'].value == "") or (nForm['mod'].value == "") .. or...
    Code (markup):
    Or should I just create a new section like the first for the new field? I do like simple, but have learned to deal with typing over the years.. lol

    Thanks again
     
    stauf, Jun 19, 2007 IP
  6. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yes, exactly, a logical OR.
    GENERALLY no parens are needed around each item:

    if (nForm['nick'].value == "" || nForm['other'].value == "" || nForm['again'].value == "")
                    {
                     alert('Please complete ALL fields.);
                     return false;
                    }
         return true;
    Code (markup):
    The logical AND is: &&

    See also, this recent post:
    http://forums.digitalpoint.com/showthread.php?p=3510862#post3510862
     
    Mike H., Jun 20, 2007 IP
  7. stauf

    stauf Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks again Mike! :cool:
     
    stauf, Jun 21, 2007 IP