Amezis 0 Report post Posted September 9, 2006 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
CrazyRob 0 Report post Posted September 9, 2006 (edited) 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 September 9, 2006 by mxweb (see edit history) Share this post Link to post Share on other sites
hype 0 Report post Posted September 9, 2006 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
masterio 0 Report post Posted September 9, 2006 (edited) 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 September 9, 2006 by masterio (see edit history) Share this post Link to post Share on other sites
Amezis 0 Report post Posted September 9, 2006 Thanks alot, I just missed the return in the onsubmit="" field ... It works now 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
darran 0 Report post Posted September 10, 2006 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
hype 0 Report post Posted September 10, 2006 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
derickkoo 0 Report post Posted November 3, 2006 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
iGuest 3 Report post Posted March 7, 2009 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
iGuest 3 Report post Posted March 12, 2009 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
iGuest 3 Report post Posted August 6, 2009 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
Jecq 0 Report post Posted August 6, 2009 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
iGuest 3 Report post Posted November 16, 2009 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
iGuest 3 Report post Posted August 19, 2010 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
iGuest 3 Report post Posted November 15, 2011 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 alotbless u Share this post Link to post Share on other sites