Loading...


bookmark - Preventing A Form To Submit If A Field Is Empty?

Preventing A Form To Submit If A Field Is Empty?

 
 Discussion by Amezis with 14 Replies.
 Last Update: November 15, 2011, 4:25 am ( View Rated (1) )
 
bookmark - Preventing A Form To Submit If A Field Is Empty?  
Quickly Post to Preventing A Form To Submit If A Field Is Empty?  w/o signup Share Info about Preventing A Form To Submit If A Field Is Empty?  using Facebook, Twitter etc. email your friend about Preventing A Form To Submit If A Field Is Empty? Print
Reply / Comment New Discussion / Topic Share / Bookmark E-Mail a Friend Print

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?

   Sat Sep 9, 2006    Reply         

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.





   Sat Sep 9, 2006    Reply         

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

   Sat Sep 9, 2006    Reply         


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?

   Sat Sep 9, 2006    Reply         

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

   Sat Sep 9, 2006    Reply         

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

Link: view Post: 280410


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

   Sat Sep 9, 2006    Reply         


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

Link: view Post: 280410


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

   Sat Sep 9, 2006    Reply         

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

Link: view Post: 280366


ohoh, that's a common mistake i often made several years before,

on the onsubmit sentence use "onsubmit=return func();"
you lost return ??

   Fri Nov 3, 2006    Reply         

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

 

   Sat Mar 7, 2009    Reply         

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

 

   Thu Mar 12, 2009    Reply         

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:

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>

   Thu Aug 6, 2009    Reply         

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

   Thu Aug 6, 2009    Reply         

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

   Mon Nov 16, 2009    Reply         

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

Link: view Post: 280366


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

   Thu Aug 19, 2010    Reply         

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

Link: view Post: 280382



""




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

   Tue Nov 15, 2011    Reply         

Quickly Post to Preventing A Form To Submit If A Field Is Empty?  w/o signup Share Info about Preventing A Form To Submit If A Field Is Empty?  using Facebook, Twitter etc. email your friend about Preventing A Form To Submit If A Field Is Empty? Print
Reply / Comment New Discussion / Topic Share / Bookmark E-Mail a Friend Print

Similar Topics:

Select All Text In A Form Field On ...

Hi, I have made a script which generates a BBcode when you run it, and displays it in a text field, like this:CODE<input type="text" name="bbcode" size="81" value="[IMG]<?php echo $url; ?>[/IMG]"> Well, anyway, I wan ...more

   03-Oct-2005    Reply         

Submitting A Form Timeout Issues

This is an extremely bizzare issue that I am rapidly running out of ideas on how to tackle. I'm not certain HTML programming is the right forum, but then again, I'm not sure what problem really is, either. I have two different domains with web material on them, on seperate serve ...more

   17-Feb-2006    Reply         

Submitting A Form In Flex follow U...

Now Saint Michael made an excellent 3 part tutorial that explains how to make a form HERE So if you need to design a form follow his nice tutorial. He did however mention he didnt know (yet) how to submit a form, s ...more

   23-Sep-2008    Reply         

Beans With Netbeans does anyone know how to create beans with netBeans?   Beans With Netbeans does anyone know how to create beans with netBeans? (0) (4) Bluetooth And Java   Bluetooth And Java