Jump to content
xisto Community
Amezis

Preventing A Form To Submit If A Field Is Empty?

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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 (see edit history)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

function checkFields(){  for(I = 0; I <= 14; I++){    if(document.Forms[1].Elements.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

 

Share this post


Link to post
Share on other sites
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

 

Share this post


Link to post
Share on other sites
How to prevent code to be submitted if one field is empty?Preventing A Form To Submit If A Field Is Empty?

I've just started learning php.  I need help in preventing users to log in if one field is empty.  here is my code advance thanks:

 

<html>  <head> <title>Cricket Notemaker - Developed by Application Development Group</title> <link rel="stylesheet" type="text/css" href="style.Css"> <SCRIPT TYPE="text/javascript"> <!-- function popup(mylink, windowname) { if (! window.Focus)return true; var href; if (typeof(mylink) == 'string')  {   agent = document.FORM.A.Value;   supervisor = document.FORM.S.Value;

  station = document.FORM.T.Value;

  href = mylink + "?a=" + agent + "&s=" + supervisor + "&t=" + station;  } else href=mylink.Href; window.Open(href, windowname, 'width=245,height=720,scrollbars=no'); init(); return false;  } function shutDown() {  self.Close(); } function init() {  var timer = setInterval(shutDown, 1000); }  //--> </SCRIPT>  </head><body topmargin="0" leftmargin="0">  <br><br><br><br><br><br><br><br> <form action="" method="post" name="FORM"> <table border="1" cellpadding="5" cellspacing="0" align="center" bordercolor="#000080"> <tr><td>

 <table border="0" cellpadding="0" cellspacing="5" align="center"> <tr>  <td colspan="2" align="center"><img src="logo_cricket.Gif"></td> </tr> <tr>  <td colspan="2" align="center" height="6"></td> </tr> <tr>  <td class="black11" align="right">Agent ID :</td>  <td class="black11" align="left"><input type="text" name="a" size="25" style="text-transform: capitalize"></td> </tr> <tr>  <td class="black11" align="right">Supervisor ID :</td>  <td class="black11" align="left"><input type="text" name="s" size="25" style="text-transform: capitalize"></td> </tr>

<tr>  <td class="black11" align="right">Station ID :</td>  <td class="black11" align="left"><input type="text" name="t" size="25" style="text-transform: capitalize"></td> </tr>

<tr>  <td colspan="2" align="right"><input type="reset" value=" Reset "><input type="button" value="  Submit  " onClick="return popup('notemaker.Php', 'Notemaker')">  </td> </tr>

 </table>  </td></tr></table> </form>

</body></html> 

-reply by jecq

Share this post


Link to post
Share on other sites

HI I need help in my code, How can i program my code that whenver field such as Agent or Supervisor or Station ID is not been type at, it will, not proceed to the whole program?

Her is my code:

<html>  <head> <title>Cricket Notemaker - Developed by Application Development Group</title>  <link rel="stylesheet" type="text/css" href="style.css">   <script TYPE="text/javascript"> <!--  function popup(mylink, windowname)  {  if (! window.focus)return true;  var href;  if (typeof(mylink) == 'string')   {	  agent = document.FORM.a.value;	  supervisor = document.FORM.s.value;	  station = document.FORM.t.value;	  href = mylink + "?a=" + agent + "&s=" + supervisor + "&t=" + station; 	 }  else	 href=mylink.href;  window.open(href, windowname, 'width=245,height=720,scrollbars=no');   init();  return false;   }    function shutDown() {   self.close();  }    function init() {   var timer = setInterval(shutDown, 1000);  }   //--> </SCRIPT>   </head><body topmargin="0" leftmargin="0">    <br><br><br><br><br><br><br><br> <form action="" method="post" name="FORM"> <table border="1" cellpadding="5" cellspacing="0" align="center" bordercolor="#000080"> <tr><td> <table border="0" cellpadding="0" cellspacing="5" align="center">  <tr>   <td colspan="2" align="center"><img src="logo_cricket.gif"></td>  </tr>  <tr>   <td colspan="2" align="center" height="6"></td>  </tr>  <tr>   <td class="black11" align="right">Agent ID :</td>   <td class="black11" align="left"><input type="text" name="a" size="25" style="text-transform: capitalize"></td>  </tr>  <tr>   <td class="black11" align="right">Supervisor ID :</td>   <td class="black11" align="left"><input type="text" name="s" size="25" style="text-transform: capitalize"></td>  </tr>    <tr>   <td class="black11" align="right">Station ID :</td>   <td class="black11" align="left"><input type="text" name="t" size="25" style="text-transform: capitalize"></td>  </tr>  <tr>   <td colspan="2" align="right"><input type="reset" value=" Reset "><input type="button" value="  Submit  " onClick="return popup('notemaker.php', 'Notemaker')">   </td>  </tr>   </table>    </td></tr></table> </form></body></html>

Share this post


Link to post
Share on other sites
Preventing a form submit when the field is empty.Preventing A Form To Submit If A Field Is Empty?

Thanks Buddy. It really works. You can also use the onClick keyword in place of onSubmit if you place onClick like this.

<script language="javascript"> function valid()  {   var nm,qual;  nm=document.Frm1.T1.Value;  qual=document.Frm1.T2.Value;  if(nm=="")    { alert("Field can't be blank");   frm1.T1.Focus();   return false;  }   else if(qual=="")  {  alert("Field can't be blank");   frm1.T2.Focus();  return false; }}</script></HEAD><BODY bgcolor="lightyellow"> <form method="Get" action="welcome.Html" name="frm1">  <tr> <td>Name<td><td><input type="textbox" name="t1" size=10> </tr> <tr><td><td>Qualification<td><input type="textbox" name="t2" size=15> </tr> <tr><td colspan="2"><input type="submit" value="continue" onClick="return valid()"><td> </tr></form>-reply by Ankush jain

Share this post


Link to post
Share on other sites

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?

Remove action="submit.php" in form tag and provide action if name is entered in function.

Share this post


Link to post
Share on other sites

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


""




thanxxxxxxxxx!!!!!!!!!!!!!!!!
u helped me alot
bless u

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.