JavaScript validation not working when checking for numeric value

I have code that always evaluates to false, even if the field is null. I only want the edit to fire if the field is not null
JavaScript error
Here's the code that's not working:
// validate Co. number is numeric
var intcheck = /^\d*$/;  

if ((ctl.form.P_CO_NBR.value < 1)
     ||(intcheck.test(ctl.form.P_CO_NBR.value) == false))
{         
   alert ('Company be numeric and greater than 0.'); 
   ctl.form.P_CO_NBR.value = ""; 
   ctl.form.P_CO_NBR.focus();  
   return false;
}
   return true;
}

Open in new window


I have similar code that works as expected - if the field is null, then nothing happens for this edit.
var intcheck = /^\d*$/;  
if ((ctl.form.P_MAX_NEW_ADVISEES[index].value < 0)
    || (intcheck.test(ctl.form.P_MAX_NEW_ADVISEES[index].value) == false))
{
   alert ('Maximum New Advisees must be numeric and greater than or equal to 0.');
   ctl.form.P_MAX_NEW_ADVISEES[index].value = "";
   ctl.form.P_MAX_NEW_ADVISEES[index].focus();
   return false;
}

Open in new window


I've tried my code at issue with [index], without [index], and with [0]
Gadsden ConsultingIT SpecialistAsked:
Who is Participating?
 
COBOLdinosaurCommented:
Form values are strings if you want to do a numeric test you need to use parseint() to create an integer equivalent.  You are getting rid of undesirable characters, but it is still a string, not a number.

Cd&
0
 
Gadsden ConsultingIT SpecialistAuthor Commented:
Ok, that makes sense, but I don't understand why the first code snippet always fires, while the second one does not if the field is blank
0
 
COBOLdinosaurCommented:
null is not the same as blank.  You could also check using the NaN type:

if isNaN(somevar) alert not a number;

Cd$
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Gadsden ConsultingIT SpecialistAuthor Commented:
Ok, that helps too. But still, the two code snippets are the same, I'm trying to figure out why one works and the other doesn't.
0
 
COBOLdinosaurCommented:
Something is different; and that is a 100% guarante.  I have no way of even guessing what that might be when all I see is a tiny fragment of out of context script.

There are well over a million lines of code in a browser, determining which ones are operating on a given fragment of random code is like trying to determine which mosquito in a swarm is going to be the one to bite you.

Cd&
0
 
Gadsden ConsultingIT SpecialistAuthor Commented:
COBOLdinosaur,

just curious - did you look at the two code snippets ? I realize this is a debugging exercise, but I've done my due diligence on troubleshooting.  And in this case, I'm pretty sure it's isolated to 22 lines of code I posted.

thx.
0
 
Gadsden ConsultingIT SpecialistAuthor Commented:
tried If (isNaN(var)) but that didn't work.

setting aside the code comparison, this is what I'm trying to do:
- if blank, keep going
- if field is not numeric or less than 1, alert for the error

given that, how would you code it ?
This doesn't work, as it croaks even if the field is blank
// validate Co. number is numeric
var intcheck = /^\d*$/;  

if ((ctl.form.P_CO_NBR.value < 1)
     ||(intcheck.test(ctl.form.P_CO_NBR.value) == false))
{         
   alert ('Company be numeric and greater than 0.'); 
   ctl.form.P_CO_NBR.value = ""; 
   ctl.form.P_CO_NBR.focus();  
   return false;
}
   return true;
}

Open in new window


Thx.
0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
not sure why it's failing, but if you're familiar with jquery whatsoever, you may want to check out the isNumeric() function. code's already been written and proven to work

http://api.jquery.com/jquery.isnumeric/
0
 
Gadsden ConsultingIT SpecialistAuthor Commented:
Big Monty,

have to dust off the cobwebs for jquery, but I'll give it a crack.

thank you.
0
 
Gadsden ConsultingIT SpecialistAuthor Commented:
slightly modified, but here's my fix.  I think the issue was "ctl.form.P_CO_NBR.value < 1"

But thanks for the assists, I resolved my issue and so picked up a few tips along the way.

Here I accept negative numbers, only integers, and also wildcard %. The negative numbers just mean the user won't get any query results.
// validate Co. number is numeric if entered

var intcheck = /^\d*$/;   
var wildcard = /[%_]/g;   

// validate Co. number is numeric if entered
if ((ctl.form.P_CO_NBR.value.replace(wildcard,"") != "")&&
    (intcheck.test(ctl.form.P_MICO_CO_NBR.value.replace(wildcard,"")) == false))
{         
   alert (''Company must be numeric.''); 
   ctl.form.P_CO_NBR.value = ""; 
   ctl.form.P_CO_NBR.focus();  
   return false;
}

Open in new window


versus the original non-working code, where I was trying to get an error if the value was < 1 or not an integer.
// validate Co. number is numeric
var intcheck = /^\d*$/;  

if ((ctl.form.P_CO_NBR.value < 1)
     ||(intcheck.test(ctl.form.P_CO_NBR.value) == false))
{         
   alert ('Company be numeric and greater than 0.'); 
   ctl.form.P_CO_NBR.value = ""; 
   ctl.form.P_CO_NBR.focus();  
   return false;
}
   return true;
}

Open in new window

0
 
Gadsden ConsultingIT SpecialistAuthor Commented:
thanks for the tips.
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.