William Peck
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
Here's the code that's not working:
I have similar code that works as expected - if the field is null, then nothing happens for this edit.
I've tried my code at issue with [index], without [index], and with [0]
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;
}
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;
}
I've tried my code at issue with [index], without [index], and with [0]
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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&
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&
ASKER
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.
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.
ASKER
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
Thx.
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;
}
Thx.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Big Monty,
have to dust off the cobwebs for jquery, but I'll give it a crack.
thank you.
have to dust off the cobwebs for jquery, but I'll give it a crack.
thank you.
ASKER
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.
versus the original non-working code, where I was trying to get an error if the value was < 1 or not an integer.
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;
}
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;
}
ASKER
thanks for the tips.
ASKER