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!
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):
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!
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
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