Solved

Simple form validation question.

Posted on 2004-08-10
5
181 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now