Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

Preventing A Form To Submit If A Field Is Empty?


14 replies to this topic

#1 Amezis

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 535 posts
  • Location:Oslo, Norway

Posted 09 September 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:
<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?

#2 CrazyRob

    ITS ALIVE.....MUHHHAAAA

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 534 posts
  • Gender:Male
  • Location:Chippenham UK
  • myCENT:63.10

Posted 09 September 2006 - 09:49 AM

Im not a super expert with javascript but try this
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.

Edited by mxweb, 09 September 2006 - 09:59 AM.


#3 hype

    Legend Killer

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 723 posts
  • Gender:Male
  • Location:Singapore
  • myCENT:40.70

Posted 09 September 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...

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

#4 masterio

    Newbie [Level 3]

  • Kontributors
  • PipPipPip
  • 49 posts

Posted 09 September 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?

Edited by masterio, 09 September 2006 - 12:57 PM.


#5 Amezis

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 535 posts
  • Location:Oslo, Norway

Posted 09 September 2006 - 05:39 PM

Thanks alot, I just missed the return in the onsubmit="" field :P... It works now :P

Masterio, I don't know how to use arrays in JS yet, so I can't answer now :)

#6 darran

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 661 posts
  • Gender:Male
  • myCENT:ZERO

Posted 10 September 2006 - 12:44 AM

View Postmasterio, on Sep 9 2006, 08:54 PM, said:

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.

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

#7 hype

    Legend Killer

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 723 posts
  • Gender:Male
  • Location:Singapore
  • myCENT:40.70

Posted 10 September 2006 - 05:30 AM

View Postmasterio, on Sep 9 2006, 08:54 PM, said:

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

#8 derickkoo

    Newbie [Level 2]

  • Kontributors
  • PipPip
  • 29 posts
  • Interests:Java Develop

Posted 03 November 2006 - 08:06 AM

View PostAmezis, on Sep 9 2006, 05:30 PM, said:

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

#9 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 07 March 2009 - 08:31 PM

function checkFields(){  for(I = 0; I <= 14; I++){    if(document.Forms[1].Elements[I].Value == ""){

//if you want to make an exception, for instance for the number 4 and 5 like in my case then just:   if(I == 4 || I == 5){ continue; }      alert("You forgott some to fill in!!");      return false;    }  }}

-reply by halim

 



#10 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 12 March 2009 - 01:54 PM

disabling submit butttonPreventing A Form To Submit If A Field Is Empty?

I'm new at jsp.  I'm trying to disable the "submit" button until my form is validated. Upon submitting my query, it would be redirected to another page. But it must get validated first. I have already written the code to validate my form.  Can anyone please help me?  Thank You. 

-reply by Luis Rodriguez

 






Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users