473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Form validation still allows submission of empty fields

29
Hi.. guys
I’m trying to use JavaScript to validate my HTML for empty fields. My code is as below. The problem is that I can escape validation and submit empty fields.

Any clue?

Expand|Select|Wrap|Line Numbers
  1.  <html> 
  2. <head>
  3. <script language="JavaScript">
  4. function isEmpty(inputStr,fieldname){
  5. if (inputStr==null || inputStr==""){
  6. alert("please fill in the box")
  7. document.form1.fieldname.focus();
  8. document.form1.fieldname.select();
  9. return false;
  10. } else {
  11. return true;
  12. }
  13.  
  14. }
  15. </script> </head>
  16. <body>
  17. <form name="form1" method="GET" action=”?????????”>
  18. <p> User ID:
  19. <input type="text" name="txt1" onBlur="isEmpty(txt1.value,txt1.name)"/> 
  20. <br />
  21. <p> User Name:
  22. <input type="text" name="txt2" onBlur="isEmpty(txt2.value,txt2.name)"/>
  23. <br /> <br />
  24. <input type="submit" VALUE="SUBMIT" />
  25. </form> </body> </html>
  26.  
Mar 22 '07 #1
13 2605
Hi.. guys
I’m trying to use JavaScript to validate my HTML for empty fields. My code is as below. The problem is that I can escape validation and submit empty fields.

Any clue?

<html>
<head>
<script language="JavaScript">
function isEmpty(inputStr,fieldname){
if (inputStr==null || inputStr==""){
alert("please fill in the box")
document.form1.fieldname.focus();
document.form1.fieldname.select();
return false;
} else {
return true;
}

}
</script> </head>
<body>
<form name="form1" method="GET" action=”?????????”>
<p> User ID:
<input type="text" name="txt1" onBlur="isEmpty(txt1.value,txt1.name)"/>
<br />
<p> User Name:
<input type="text" name="txt2" onBlur="isEmpty(txt2.value,txt2.name)"/>
<br /> <br />
<input type="submit" VALUE="SUBMIT" />
</form> </body> </html>
You had given wrong
path in action=”?????????”
use like this action="".

Akhilesh
Mar 22 '07 #2
elsheh
29
Thanx
The problem is not because of the action attribute, you may eliminate it. The issue is how to prevent of submission empty fields. (got it?)
Cheers
You had given wrong
path in action=”?????????”
use like this action="".

Akhilesh
Mar 22 '07 #3
acoder
16,027 Expert Mod 8TB
Instead of passing the name, pass the object itself, e.g. txt1. Then in your function, in place of
Expand|Select|Wrap|Line Numbers
  1. document.form1.fieldname.focus()
you can have
Expand|Select|Wrap|Line Numbers
  1. fieldname.focus()
because fieldname is the text box object.
Mar 22 '07 #4
elsheh
29
thanx acoder

but still doesn't work properly.I mean, i still able to submit empty fields.
plz help

Instead of passing the name, pass the object itself, e.g. txt1. Then in your function, in place of
Expand|Select|Wrap|Line Numbers
  1. document.form1.fieldname.focus()
you can have
Expand|Select|Wrap|Line Numbers
  1. fieldname.focus()
because fieldname is the text box object.
Mar 22 '07 #5
iam_clint
1,208 Expert 1GB
[HTML]
<html>
<head>
<script language="JavaScript">
function isEmpty(inputStr,fieldname){
if (inputStr==null || inputStr==""){
alert("please fill in the box")
document.form1.fieldname.focus();
document.form1.fieldname.select();
return false;
} else {
return true;
}
}
function validate() {
//check for blank input boxes here
//possibly alert("hey you didn't fill in everything"); and return false; if there is blank boxes (stops the submission);
//else return true
}
</script> </head>
<body>
<form name="form1" method="GET" onsubmit="validate();" action=”?????????”>
<p> User ID:
<input type="text" name="txt1" onBlur="isEmpty(txt1.value,txt1.name)"/>
<br />
<p> User Name:
<input type="text" name="txt2" onBlur="isEmpty(txt2.value,txt2.name)"/>
<br /> <br />
<input type="submit" VALUE="SUBMIT" />
</form> </body> </html>
[/HTML]

Changed title of thread to more suit the question being asked
Mar 22 '07 #6
Hi.. guys
I’m trying to use JavaScript to validate my HTML for empty fields. My code is as below. The problem is that I can escape validation and submit empty fields.

Any clue?

<html>
<head>
<script language="JavaScript">
function isEmpty(inputStr,fieldname){
if (inputStr==null || inputStr==""){
alert("please fill in the box")
document.form1.fieldname.focus();
document.form1.fieldname.select();
return false;
} else {
return true;
}

}
</script> </head>
<body>
<form name="form1" method="GET" action=”?????????”>
<p> User ID:
<input type="text" name="txt1" onBlur="isEmpty(txt1.value,txt1.name)"/>
<br />
<p> User Name:
<input type="text" name="txt2" onBlur="isEmpty(txt2.value,txt2.name)"/>
<br /> <br />
<input type="submit" VALUE="SUBMIT" />
</form> </body> </html>
you are doing validation on onBlur event of textbox,
If you want to validate on onSubmit event of form then
you need to do like this.
<form name="form1" method="GET" action=”?????????” onSubmit="validate()" >
and then make a validate() function in javascript.
Mar 23 '07 #7
Logician
210 100+
you are doing validation on onBlur event of textbox,
If you want to validate on onSubmit event of form then
you need to do like this.
<form name="form1" method="GET" action=”?????????” onSubmit="validate()" >
and then make a validate() function in javascript.
Expand|Select|Wrap|Line Numbers
  1. <form name="form1" method="GET" action=”?????????” onSubmit="return  validate()" >
Mar 23 '07 #8
elsheh
29
how about parameters?
cheers
Expand|Select|Wrap|Line Numbers
  1. <form name="form1" method="GET" action=”?????????” onSubmit="return  validate()" >
Mar 23 '07 #9
acoder
16,027 Expert Mod 8TB
See this link.
Mar 23 '07 #10
elsheh
29
thanx acoder
i still unable to solve this problem. could you help plz.
cheers
See this link.
Mar 24 '07 #11
ssh
6
Hi.. guys
I’m trying to use JavaScript to validate my HTML for empty fields. My code is as below. The problem is that I can escape validation and submit empty fields.

Any clue?

Expand|Select|Wrap|Line Numbers
  1.  <html> 
  2. <head>
  3. <script language="JavaScript">
  4. function isEmpty(inputStr,fieldname){
  5. if (inputStr==null || inputStr==""){
  6. alert("please fill in the box")
  7. document.form1.fieldname.focus();
  8. document.form1.fieldname.select();
  9. return false;
  10. } else {
  11. return true;
  12. }
  13.  
  14. }
  15. </script> </head>
  16. <body>
  17. <form name="form1" method="GET" action=”?????????”>
  18. <p> User ID:
  19. <input type="text" name="txt1" onBlur="isEmpty(txt1.value,txt1.name)"/> 
  20. <br />
  21. <p> User Name:
  22. <input type="text" name="txt2" onBlur="isEmpty(txt2.value,txt2.name)"/>
  23. <br /> <br />
  24. <input type="submit" VALUE="SUBMIT" />
  25. </form> </body> </html>
  26.  
hi!
just try this one!!!!
nBlur="isEmpty(this,txt1.name)";

function isEmpty(inputStr,fieldname){
if (inputStr.value.length==0 || inputStr==""){
alert("please fill in the box")
}

Regards,
ssh
Mar 24 '07 #12
acoder
16,027 Expert Mod 8TB
hi!
just try this one!!!!
nBlur="isEmpty(this,txt1.name)";

function isEmpty(inputStr,fieldname){
if (inputStr.value.length==0 || inputStr==""){
alert("please fill in the box")
}

Regards,
ssh
Whilst in principle this may work (though in this case it won't), it can be extremely annoying to have an alert onblur. Blurring can occur without user intervention, e.g. another program gains focus. An error message next to the field is much better. On the other hand, if it's an onsubmit or even onchange error check, then an alert should be fine.
Mar 26 '07 #13
acoder
16,027 Expert Mod 8TB
how about parameters?
cheers
In your validate() function, you don't need parameters. Just reference the text fields using their names. If you want to, you can pass the form reference as "this":
Expand|Select|Wrap|Line Numbers
  1. return validate(this);
Then in your validate function:
Expand|Select|Wrap|Line Numbers
  1. function validate(form) {
  2.  var txt1 = form.txt1;
  3.  if (txt1.value=="") {
  4.    alert ("Please fill in the box");
  5.    txt1.focus();
  6.    return false;
  7.  }
  8.  .... // txt2 and any other text boxes
  9.  return true;
  10. }
Mar 26 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Charles Banas | last post by:
weird subject - i hope more than just one curious regular will hear me out. :) ok, i've got a bit of a big problem, and i need answers as soon as possible. i know this forum is meant for web...
4
by: bnp | last post by:
Hi All, I am quite new the JavaScript. Basically I am a C++ programmer, but now I am working on JavaScript since last 5 days. I have a problem regarding the form validation. I have created a...
2
by: Tim Mills | last post by:
The following code asks the user to sumbit a name, email address, and some text for a quotation via a FORM. I have written a javascript function to evaluate the fields in the form and pop-up a...
9
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be...
7
by: h7qvnk7q001 | last post by:
I'm trying to implement a simple server-side form validation (No Javascript). If the user submits a form with errors, I want to redisplay the same form with the errors highlighted. Once the form...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
2
by: ddog | last post by:
I have a form with 3 text fields (one of which is a zip code) and 5 combo boxes. The combo boxes are all set with the first value as 'selected' when the page is first displayed. The 3 text fields...
7
ak1dnar
by: ak1dnar | last post by:
Hi, I got this scripts from this URL There is Error when i submit the form. Line: 54 Error: 'document.getElementbyID(....)' is null or not an object What is this error. Complete Files
12
by: Gustaf | last post by:
I've been working on a membership form for a while, and find it very tedious to get an acceptable level of form validation. A web search for solutions revealed some home-brewed solutions, such as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.