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

#11 iGuest

    Hail Caesar!

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

Posted 06 August 2009 - 06:42 PM

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

#12 Jecq

    Newbie

  • Kontributors
  • Pip
  • 1 posts

Posted 06 August 2009 - 10:34 PM

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>


#13 iGuest

    Hail Caesar!

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

Posted 16 November 2009 - 10:32 AM

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



#14 Guest_Shantan_*

  • Guests

Posted 19 August 2010 - 09:12 AM

View PostAmezis, on 09 September 2006 - 09:30 AM, 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?

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

#15 Guest_shilo_*

  • Guests

Posted 15 November 2011 - 12:25 PM

View Posthype, on 09 September 2006 - 10:27 AM, said:

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




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