Jul 25, 2008

Preventing A Form To Submit If A Field Is Empty?

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > Java, Java Servlets, Java Script, & JSP

free web hosting

Preventing A Form To Submit If A Field Is Empty?

Amezis
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?

 

 

 


Reply

CrazyRob
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.

 

 

 


Reply

hype
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... smile.gif

Reply

masterio
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?

Reply

Amezis
Thanks alot, I just missed the return in the onsubmit="" field huh.gif... It works now smile.gif

Masterio, I don't know how to use arrays in JS yet, so I can't answer now unsure.gif

Reply

darran
QUOTE(masterio @ Sep 9 2006, 08: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?


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 wink.gif

Reply

hype
QUOTE(masterio @ Sep 9 2006, 08: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?


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... laugh.gif

Reply

derickkoo
QUOTE(Amezis @ Sep 9 2006, 05:30 PM) *

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 ??

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Recent Queries:-
  1. dont submit form if input blank - 2.27 hr back. (1)
  2. empty form submit - 7.74 hr back. (1)
  3. javascript if document.form.name = name - 20.87 hr back. (1)
  4. prevent submit the empty form in php - 28.78 hr back. (1)
  5. onsubmit prevent submit - 31.60 hr back. (1)
  6. php know if a form field is filled - 33.17 hr back. (1)
  7. show how to submit a form in jsp - 33.76 hr back. (1)
  8. get document.form.name.value at a particular index - 35.49 hr back. (1)
  9. document.form.name - 36.09 hr back. (1)
  10. prevent submitting form array javascript - 44.67 hr back. (1)
  11. submitting field arrays javascript - 44.70 hr back. (1)
  12. checking if field is empty before submitting with php - 49.09 hr back. (1)
  13. empty php form field alert - 52.07 hr back. (1)
  14. prevent blank fields in php forms - 53.46 hr back. (1)
Similar Topics

Keywords : preventing, form, submit, field, empty

  1. Preventing People From Copying Your Javascript
    (7)
  2. Form Problem
    prevent submit if value="examplevalue" (6)
    i would like to have a code inserted to a form that prevents submitting if one of the inputfields
    (in particular) contains the value: "examplevalue" . Does anyone know the solution to this? btw:
    the texts in other inputfields shouldn't be cleared if possible.. thanks in advance
    /cool.gif' border='0' style='vertical-align:middle' alt='cool.gif' /> ....
  3. Submit Form Thorugh Jum Menu
    (0)
    is it possible to submit a form if you click one of the options of a select drop down in the form,
    but each depending on which option you choose, the form is submitted to a different location. kinda
    like a jump menu submitting the form, to different 'locations' kvarner....

    1. Looking for preventing, form, submit, field, empty

Searching Video's for preventing, form, submit, field, empty
advertisement



Preventing A Form To Submit If A Field Is Empty?



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE