Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Check if empty problem

Posted on 2004-04-14
12
Medium Priority
?
1,085 Views
Last Modified: 2012-06-27
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?
0
Comment
Question by:drakkarnoir
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +4
12 Comments
 
LVL 11

Expert Comment

by:sciber_dude
ID: 10828549
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
 
LVL 11

Expert Comment

by:sciber_dude
ID: 10828557
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
 

Author Comment

by:drakkarnoir
ID: 10828697
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:James Looney
ID: 10828885
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
 
LVL 30

Expert Comment

by:third
ID: 10828998
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
 
LVL 12

Expert Comment

by:lil_puffball
ID: 10829259
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
 
LVL 12

Accepted Solution

by:
lil_puffball earned 2000 total points
ID: 10829506
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
 
LVL 11

Expert Comment

by:sciber_dude
ID: 10830085
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
 
LVL 7

Expert Comment

by:gam3r_3xtr3m3
ID: 10831604
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
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10833460
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
 

Author Comment

by:drakkarnoir
ID: 10834324
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
 
LVL 12

Expert Comment

by:lil_puffball
ID: 10836545
Thanks drakkarnoir for the points and the A. :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…

618 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