|
|
|
|
![]() ![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members
Posts: 535 Joined: 14-February 05 From: Oslo, Norway Member No.: 3,759 |
Post
#1
Sep 9 2006, 09:30 AM
Well, I've just started learning Javascript, so I'm no wiz yet... Anyways, I have a form where I want to show an error message when the user has not filled out the "name" field. My code looks like this: CODE <script type="text/javascript" language="JavaScript"> function nameempty() { if ( document.form.name.value == '' ) { alert('No name was entered!') return false; } } </script> <form action="submit.php" method="post" name="form" onSubmit="nameempty();"> <input type="text" name="name" class="textfield"> <input type="submit" value="Submit" name="submit"> </form> With this code, it alerts the user when the form is empty, but still submits it when the user clicks [OK]. What am I doing wrong? |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members
Posts: 531 Joined: 17-October 05 From: Chippenham UK Member No.: 13,031 |
Post
#2
Sep 9 2006, 09:49 AM
Im not a super expert with javascript but try this CODE script type="text/javascript" language="JavaScript"> function submit_form(value) { if(document.form.name.value == "") { alert("No name was enterd."); value.form.name.select(); return false; </script> <form action="submit.php" method="post" name="form" onSubmit="nameempty();"> <input type="text" name="name" class="textfield"> <input type="submit" value="Submit" name="submit"> </form> i added in a few extra lines i don't know if this works as i said im not a huge expert with javascript but try it. This post has been edited by mxweb: Sep 9 2006, 09:59 AM |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members
Posts: 678 Joined: 15-April 05 From: Singapore Member No.: 5,697 |
Post
#3
Sep 9 2006, 10:27 AM
If I'm not wrong there's one fatal mistake in your form validation... You did the onsubmit="" wrongly, you forgotten to place a return word there... It should be as followed... CODE <script type="text/javascript" language="JavaScript"> function nameempty() { if ( document.form.name.value == '' ) { alert('No name was entered!') return false; } } </script> <form action="submit.php" method="post" name="form" onSubmit="return nameempty();"> <input type="text" name="name" class="textfield"> <input type="submit" value="Submit" name="submit"> </form> Try It and tell me if it works... |
![]() ![]() ![]() Group: Members
Posts: 49 Joined: 25-August 06 Member No.: 28,897 |
Post
#4
Sep 9 2006, 12:54 PM
does anyone know how chek alot of fields with javascript? I'm newbie in javascript, how to pass fieldname to an array? should we use getElementById or shomething?
This post has been edited by masterio: Sep 9 2006, 12:57 PM |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members
Posts: 535 Joined: 14-February 05 From: Oslo, Norway Member No.: 3,759 |
Post
#5
Sep 9 2006, 05:39 PM
Thanks alot, I just missed the return in the onsubmit="" field Masterio, I don't know how to use arrays in JS yet, so I can't answer now |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Post
#6
Sep 10 2006, 12:44 AM
QUOTE(masterio @ Sep 9 2006, 08:54 PM) [snapback]280410[/snapback] does anyone know how chek alot of fields with javascript? I'm newbie in javascript, how to pass fieldname to an array? should we use getElementById or shomething? Yes you can use the method getElementById() Why getElementById() ? When the web document is generated, there are so many methods which can be used belonging to the document object. Since you want to use an array to control validation of all your fields, you can opt to use a String array whereby you input all the names of your fields like txtName, or whatever you call your textboxes. And another array whereby you store your error messages. Using a for loop to loop through all the values in the String array. CODE (for int i=0; i<aryFieldName.length; i++) { /* This is where you can pass in your String array values into something like this */ var objControl; objControl = window.document.getElementById(aryFieldName[i]); intValue = objControl.value; if (intValue = "") { alert(aryErrorMsg[i]); } } This is how I feel things should be done, feel free to try it. If you have a better solution do propose it |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members
Posts: 678 Joined: 15-April 05 From: Singapore Member No.: 5,697 |
Post
#7
Sep 10 2006, 05:30 AM
QUOTE(masterio @ Sep 9 2006, 08:54 PM) [snapback]280410[/snapback] does anyone know how chek alot of fields with javascript? I'm newbie in javascript, how to pass fieldname to an array? should we use getElementById or shomething? Checking a lots of field, use a lot of the if else method lol... Passing fieldname to an array, hmm... Did you want to use array to help you in your form validation? I'm not too good in arrays and I dont want to give the wrong advice... |
![]() ![]() Group: Members
Posts: 29 Joined: 3-November 06 Member No.: 32,672 |
Post
#8
Nov 3 2006, 08:06 AM
QUOTE(Amezis @ Sep 9 2006, 05:30 PM) [snapback]280366[/snapback] Well, I've just started learning Javascript, so I'm no wiz yet... Anyways, I have a form where I want to show an error message when the user has not filled out the "name" field. My code looks like this: CODE <script type="text/javascript" language="JavaScript"> function nameempty() { if ( document.form.name.value == '' ) { alert('No name was entered!') return false; } } </script> <form action="submit.php" method="post" name="form" onSubmit="nameempty();"> <input type="text" name="name" class="textfield"> <input type="submit" value="Submit" name="submit"> </form> With this code, it alerts the user when the form is empty, but still submits it when the user clicks [OK]. What am I doing wrong? ohoh, that's a common mistake i often made several years before, on the onsubmit sentence use "onsubmit=return func();" you lost return ?? |
![]() ![]() |
Similar Topics
| Topic Title | Replies | Topic Starter | Views | Last Action | |||
|---|---|---|---|---|---|---|---|
![]() |
6 | LuciferStar | 607 | 7th November 2004 - 12:40 PM Last post by: eldeo |
|||
![]() |
5 | odomike | 345 | 27th October 2004 - 02:29 AM Last post by: odomike |
|||
![]() |
6 | wild20 | 488 | 8th June 2006 - 10:04 PM Last post by: wild20 |
|||
![]() |
1 | allfanged1 | 941 | 14th June 2006 - 04:23 PM Last post by: AnGeL KiSS |
|||
![]() |
3 | msabas | 477 | 18th July 2006 - 05:58 PM Last post by: msabas |
|||
![]() |
7 | cragllo | 936 | 14th December 2004 - 01:51 AM Last post by: khmerneed |
|||
![]() |
4 | Mario | 664 | 29th November 2004 - 03:40 PM Last post by: WeBmAsTeR |
|||
![]() |
0 | thablkpanda | 954 | 7th December 2004 - 01:25 AM Last post by: thablkpanda |
|||
![]() |
2 | s243a | 451 | 8th December 2004 - 03:24 AM Last post by: thablkpanda |
|||
![]() |
2 | FaLgoR | 685 | 4th January 2005 - 06:30 PM Last post by: FaLgoR |
|||
![]() |
1 | kkrizka | 557 | 21st September 2007 - 07:21 PM Last post by: delivi |
|||
![]() |
2 | GuySpook | 927 | 6th February 2005 - 09:35 PM Last post by: SilentWarrior |
|||
![]() |
12 | Neutrality | 1,370 | 31st August 2006 - 10:49 AM Last post by: goldrain |
|||
![]() |
5 | Peaktao | 371 | 26th February 2005 - 04:35 AM Last post by: no9t9 |
|||
![]() |
0 | RGPHNX | 568 | 27th February 2005 - 08:38 PM Last post by: RGPHNX |
|||
|
Lo-Fi Version | Time is now: 5th December 2008 - 04:14 AM |