Check if empty problem

function test(obj,name)
{
if(!obj.value && strhello)
{
alert('Please enter a value for the '+name+'.');
obj.focus();
return false;
}

I'm passing it like this:

onBlur="test(this,'Field Name');

But I have it on multiple fields, and if I leave one field blank, it will go into an endless loop alerting for all remaining fields that are empty. I tried adding a "return false/true" at the end of the event handler, but no luck. Any ideas?
drakkarnoirAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
lil_puffballConnect With a Mentor Commented:
Actually there was a small problem with that one, this should do it:

<script>
var check=true;
function checkEmpty(obj,name){
  if(check&&obj.value.replace(/\s+/gi,"")==""){
    alert('Please enter a value for the '+name+'.');
    obj.focus();
    check=false;
    setTimeout("check=true",10);
  }
}
</script>

<form>
<input type=text onblur="checkEmpty(this,'Field 1');">
<input type=text onblur="checkEmpty(this,'Field 2');">
</form>
0
 
sciber_dudeCommented:
Hi drakkarnoir,

Try this.

function test(obj)
{
if (obj.value != "")
 {
   if (!obj.value && strhello)
   {
      alert('Please enter a value for the '+obj.name+'.');
      obj.focus();
      return false;
   }
 }
}
I'm passing it like this:

onBlur="test(this);"


:) SD
0
 
sciber_dudeCommented:
I am not sure why you need this line

> if (!obj.value && strhello)

but i have retained it coz i dont know what strhello is.

If you dont need it, you can get rid of it.

:) SD
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
drakkarnoirAuthor Commented:
if (obj.value != "")
 {
   if (!obj.value && strhello)

Maybe I don't understand this...you're first saying, to see if the value is not blank, then go into the next control saying if the value is blank? That seems to make no sense to me. Also, strhello is not needed. I tried it this way:

if (obj.value == "")
 {
    if(!obj.value)
    {
    alert('Please enter a value for the '+name+'.');
    obj.focus();
    }
}

And it kept doing the same thing. I think the problem is, since the event handler is onBlur, if the user leaves the first field empty, and then clicks on the next field, it will activate the alert, but at the same time it's also blurring off the next field, which is empty...and so it goes into this endless loop...
0
 
James LooneySr. Programmer/AnalystCommented:
function test(obj,name)
{
   if(obj.value == null || obj.value == "")
   {
      alert('Please enter a value for the '+name+'.');
      obj.focus();
      return false;
   }
}

<input type="text" name="Field Name" onBlur="test(this,'Field Name');">
0
 
thirdCommented:
onblur is annoying. validate your values on the onsubmit event of the form instead. can't you notice that most of the sites prefere validating during the onsubmit event?

more convenient.  
0
 
lil_puffballCommented:
Like third said, this is very annoying, but if you must do it this way, here's a script that will work (by the way, this also checks to see if the user only entered spaces--the other way, the user could enter a space and the script wouldn't catch it):

<script>
var alreadyChecked=false;
function checkEmpty(obj){
  if(alreadyChecked){alreadyChecked=false;return;}
  if(!alreadyChecked&&obj.value.replace(/\s+/gi,"")==""){
    alert('Please enter a value for the '+name+'.');
    obj.focus();
    alreadyChecked=true;
  }
}
</script>

<form>
<input type=text onblur="checkEmpty(this,'Field 1');">
<input type=text onblur="checkEmpty(this,'Field 2');">
</form>
0
 
sciber_dudeCommented:
oops! sorry about that! I was not thinking.

if (obj.value == "")
 {
    alert('Please enter a value for the '+name+'.');
    obj.focus();
}

should do the trick! Jimbo95's solution is much better. Here is a small modification to that...

~~~~~~~~~~~~~~~~~~~~~~~~~~~
function test(obj)
{
   if(obj.value == null || obj.value == "")
   {
      alert('Please enter a value for the '+obj.name+'.');
      obj.focus();
      return false;
   }
}

<input type="text" name="Field Name" onBlur="test(this);">                     << here is the modification

This way you dont have to type the Field Name for each of the <input>s
:) SD
0
 
gam3r_3xtr3m3Commented:
much better:

<script>function chkObj(obj,str){if(obj.value==""){alert("Please enter a value for the "+str+".");obj.focus();}}</script>
<input type="text" onBlur="chkObj(this,'Field 1');">    
<input type="text" onBlur="chkObj(this,'Field 2');">
<input type="text" onBlur="chkObj(this,'Field 3');">
<input type="text" onBlur="chkObj(this,'Field 4');">
0
 
jaysolomonCommented:
My proposal, it checks for continuous spaces

<html>
<head>
<title></title>
<script type="text/javascript">
<!--
function hasValue(strFieldName,strMsg){
      var strValue = strFieldName.value;
            strValue = strValue.split(" ").join("");
            if(strValue == ""){
                  alert("Please enter a value for the "+ strMsg +".");
                        strFieldName.select();
                              strFieldName.focus();
                                    return false;
            }
      return true;
}
// -->
</script>
</head>
<body>
<form name="form1" method="post" action="">
      <input type="text" name="fName" onblur="return hasValue(this,'First Name');">
      <input type="text" name="lName" onblur="return hasValue(this,'Last Name');">
</form>
</body>
</html>

0
 
drakkarnoirAuthor Commented:
Thanks for all the input...but most of the answers keep missing the basic problem, that I need to somehow tell it to stop after it does 1 onBlur attempt, it will invoke the function yet again, which was what I was trying to get around.

lil_puffball fixed that problem with his solution, hence why I picked his solution. However, thanks to all those who participated :)
0
 
lil_puffballCommented:
Thanks drakkarnoir for the points and the A. :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.