[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

onBlur Javascript Validation looking for NULL value...

Posted on 2006-05-06
3
Medium Priority
?
336 Views
Last Modified: 2010-05-18
 I have a javascript function that's currently working for some basic validation of the client.  It will return false for anything that's not a number, including blank spaces,  which is perfect.  However, it's ignoring null or empty values.  

Here's the function:

function validateNumber() {
    // Get the source element.
     var el = event.srcElement;
     // Valid numbers
     var num = "0123456789";
     event.returnValue = true;
     /* Loop over contents. If any character is not a number,
        set the return value to false. */
     for (var intLoop = 0;
        intLoop < el.value.length; intLoop++)
        if (-1 == num.indexOf(el.value.charAt(intLoop))) {
           event.returnValue=false;
           alert("You MUST enter a valid quantity in the box.")
        }
        if (!event.returnValue)       // Bad value
           el.className = "badValue"; // Change form element background class.
        else
         // Clear class to use default rendering.
         el.className="";
 }

I'm sure it's something simple I'm missing.  

Thanks in advance.

Joe Jenkins
0
Comment
Question by:Joe Jenkins
  • 2
3 Comments
 
LVL 17

Accepted Solution

by:
Cem Türk earned 1000 total points
ID: 16623173
function validateNumber() {
    // Get the source element.
     var el = event.srcElement;
     // Valid numbers
     var num = "0123456789";
     event.returnValue = true;
if( el.value.length==0) { //if null is entered length will be 0
alert("You MUST enter a quantity in the box");
     event.returnValue=false;
}
     /* Loop over contents. If any character is not a number,
        set the return value to false. */
     for (var intLoop = 0;
        intLoop < el.value.length; intLoop++)
        if (-1 == num.indexOf(el.value.charAt(intLoop))) {
           event.returnValue=false;
           alert("You MUST enter a valid quantity in the box.")
        }
        if (!event.returnValue)       // Bad value
           el.className = "badValue"; // Change form element background class.
        else
         // Clear class to use default rendering.
         el.className="";
 }
0
 
LVL 5

Author Comment

by:Joe Jenkins
ID: 16623204
Absolutely perfect.  I knew it was something simple.  Saturday evening brain fart!  Here's the final script with your changes and some symantical changes to make more sense event order.

function validateNumber() {
    // Get the source element.
     var el = event.srcElement;
     // Valid numbers
     var num = "0123456789";
     event.returnValue = true;
if( el.value.length==0) { //if null is entered length will be 0
     event.returnValue=false;
}
     /* Loop over contents. If any character is not a number,
        set the return value to false. */
     for (var intLoop = 0;
        intLoop < el.value.length; intLoop++)
        if (-1 == num.indexOf(el.value.charAt(intLoop)))
           event.returnValue=false;
          if (!event.returnValue) {      // Bad value
              el.className = "badValue"; // Change class.
              alert("You MUST enter a value in the box \"0\" if NONE.");
              el.focus();
          }
            else
               // Clear class to use default rendering.
               el.className="";
}

With the way it's written now, chnages the background color of the element and returns focus to it so the user ABSOLUTELY knows where the issue is.  

Used in combination with:
ONKEYPRESS="if ((event.keyCode < 46) || (event.keyCode > 57) || (event.keyCode == 47)) event.returnValue = false;"

The user has no option but to put in a numeric value requiring an absolute number with every field.  

Thanks again Turk.

Points awarded.

Joe Jenkins
0
 
LVL 17

Expert Comment

by:Cem Türk
ID: 16623272
your welcome
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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'…
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…
Suggested Courses

872 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