Solved

Simple form validation question.

Posted on 2004-08-10
5
183 Views
Last Modified: 2008-02-26
Instead of me diving in books and learn JavaScipt, with which I don't have the time.  I just need a simple validation solution.

I have a few fields in my form, and would like to validate if they are blank or empty.

Here is some code I am testing with:
<html>
<head>
<script type='text/javascript'>
function checkRequired(){
if (form.empName.value == "")
{alert("You have left a required field blank.");}
}
</script>
</head>
<body>
<form>
<input name=empName type='text' onblur=checkRequired()>
<input name=empPhone type='text' onblur=checkRequired()>
<input name=empEmail type='text' onblur=checkRequired()>
</form>
</body>
</html>

What I want to do is be able to send the name of the field into the javascript so it can check that field, if it is blank I want it to give the alert, and set focus back on that field.  That is all, I have seen this code work before, but can't recall where.  I said it was simple.  Help would be greatly appreciated, and thank you.
0
Comment
Question by:Meps
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:mattfairw
ID: 11762545
a better method would be to check on submit:

<html>
<head>
<script type='text/javascript'>
function checkRequired(theForm){
if (theForm.empName.value == "" || theForm.empPhone.value == "" || theForm.empEmail.value == "")
{
alert("You have left a required field blank.");
return false;
}
return true;
}
</script>
</head>
<body>
<form action="newPage.ext" onSubmit="return checkRequired(this)>
<input name=empName type='text'>
<input name=empPhone type='text'>
<input name=empEmail type='text'>
</form>
</body>
</html>
0
 
LVL 3

Expert Comment

by:mattfairw
ID: 11762577
and a little modification (i left out a quote and a submit button ;) )

<html>
<head>
<script type='text/javascript'>
function checkRequired(theForm){
if (theForm.empName.value == "" || theForm.empPhone.value == "" || theForm.empEmail.value == "")
{
alert("You have left a required field blank.");
return false;
}
return true;
}
</script>
</head>
<body>
<form action="newPage.ext" onSubmit="return checkRequired(this)">
<input name=empName type='text'>
<input name=empPhone type='text'>
<input name=empEmail type='text'>
<input type="submit" name="subBtn" value="Submit" />
</form>
</body>
</html>

-Matt
0
 
LVL 3

Author Comment

by:Meps
ID: 11762642
I wish I could use onSubmit, but my real page causes this to be an impossiblilty.

Here is what I have so far.
<SCRIPT>
function checkRequired(field){
if (field.value == "")
{alert("You have left a required field blank. \n Please ensure all fields are completed.");
field.focus();}
}
</SCRIPT>

The only problem is that if the customer clicks on the name field, then tabs over to another field, you run into a loop where both the name and the phone switch off with the error.

I saw this site, but I don't understand where the timeout comes into play.
http://forums.aspfree.com/t22355/s.html
0
 
LVL 3

Accepted Solution

by:
mattfairw earned 35 total points
ID: 11763059
alright, i wasn't sure how that site was doing it either, but i solved it by using a global variable to do a pseudo test if a field lost focus due to the function:

<html>
<head>
<SCRIPT>
var globalCheckVar = 1;
function checkRequired(field){
  if (field.value == "" && globalCheckVar)
  {
    alert("You have left a required field blank. \n Please ensure all fields are completed.");
    globalCheckVar = 0;
    field.focus();
  }
  else
  {
  globalCheckVar = 1;
  }
}
</SCRIPT>
</head>
<body>
<form>
<input name=empName type='text' onBlur="checkRequired(this)" size="20">
<input name=empPhone type='text' onBlur="checkRequired(this)"size="20">
<input name=empEmail type='text' onBlur="checkRequired(this)"size="20">
</form>
</body>
</html>

-Matt
0
 
LVL 3

Author Comment

by:Meps
ID: 11776336
Thanks a lot for the help.  It worked very well.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question