Link to home
Start Free TrialLog in
Avatar of William Peck
William PeckFlag for United States of America

asked on

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
User generated image
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]
ASKER CERTIFIED SOLUTION
Avatar of COBOLdinosaur
COBOLdinosaur
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of William Peck

ASKER

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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&
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.
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.
SOLUTION
Avatar of Big Monty
Big Monty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Big Monty,

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

thank you.
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

thanks for the tips.