|
|
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) ) | |||
![]() |
|
|
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?
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.
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...
Masterio, I don't know how to use arrays in JS yet, so I can't answer now
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
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...
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 ??
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
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
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>
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
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
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.
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
Similar Topics:
Select All Text In A Form Field On ...
Submitting A Form Timeout Issues
Submitting A Form In Flex follow U...
Beans With Netbeans does anyone know how to create beans with netBeans? (0)
|
(4) Bluetooth And Java
|
Loading...
HOME 





WebPlus X4 - Creating a script to process your forms data
Lookup Bad Email Addresses for ACT! by Sage CRM - Facebook Fan Gift
Preventing Adverse Health Effects from Nanotechnology
Preventing Dropout and Increasing Graduation Rates
Preventing Stimulus Waste and Fraud: Who are the Watchdogs? (Part 2)

