Nov 8, 2009
Pages: 1, 2

Preventing A Form To Submit If A Field Is Empty?

free web hosting

Read Latest Entries..: (Post #11) by Jecq on Aug 6 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: 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"> <!--  func...
read more.
Read the FIRST post of this Topic. - Express your Opinion! Contribute Knowledge :-).

Open Discussion > MODERATED AREA > Computers > Programming Languages > Java, Java Servlets, Java Script, & JSP

Preventing A Form To Submit If A Field Is Empty?

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?

 

 

 


Comment/Reply (w/o sign-up)

mxweb
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.

 

 

 


Comment/Reply (w/o sign-up)

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... smile.gif

Comment/Reply (w/o sign-up)

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?

Comment/Reply (w/o sign-up)

Amezis
Thanks alot, I just missed the return in the onsubmit="" field huh.gif... It works now smile.gif

Masterio, I don't know how to use arrays in JS yet, so I can't answer now unsure.gif

Comment/Reply (w/o sign-up)

darran
QUOTE(masterio @ Sep 9 2006, 08: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?


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 wink.gif

Comment/Reply (w/o sign-up)

hype
QUOTE(masterio @ Sep 9 2006, 08: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?


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... laugh.gif

Comment/Reply (w/o sign-up)

derickkoo
QUOTE(Amezis @ Sep 9 2006, 05:30 PM) *

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?


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

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

Comment/Reply (w/o sign-up)

(G)halim

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

 


Comment/Reply (w/o sign-up)

(G)Luis Rodriguez
disabling submit buttton
Preventing 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

 


Comment/Reply (w/o sign-up)

Latest Entries

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



Comment/Reply (w/o sign-up)



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Pages: 1, 2

Searching Video's for preventing, form, submit, field, empty
See Also,
advertisement


Preventing A Form To Submit If A Field Is Empty?

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com