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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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 MontyWeb Ninja at largeCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.