Solved

Check if empty problem

Posted on 2004-04-14
12
1,061 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
  • 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 12

Accepted Solution

by:
lil_puffball earned 500 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
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…

757 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

19 Experts available now in Live!

Get 1:1 Help Now