I have a problem in asp application validation. My textbox only accepts numeric value…
It should not accept empty field and negative values and charcters…
I got it for empty field…
but not for numeric value…
function providerid(fld){
var error=" "
if(fld.value== ""){
fld.style.background='Yellow';
error= "You didn't enter a product id \
";
}else{
fld.style.background='White';
}return error;
}
function validateFormOnSubmit(form1) {
reason += providerid(form1.pid);
if (reason != "") {
alert("fields neccessary:\
" + reason);
return false;
}
return true;
}
I am not aware of IsNan function…
Can anyone give some idea about numeric validation…
console.log (isNaN('abc')); // true
!isNaN - (!)Not not a number. In other words 'is a number'
console.log (!isNaN('12')); // true
console.log (!isNaN('-12')); // true, which you don't want.
Using a regular expression to test if anything other than 0 to 9 or a dot(decimal place) are present.
var isNum = function(num){return !/[^.[0-9]]*/.test(num);};
Is there any situation where the isNaN check is needed? Those values will just just fail the condition when checked too, right?
The only other thing to consider is when an empty string is involved, in which case Number converts that to 0. So you would need a separate check if 0 is a valid number too.
If you don’t intend to allow 0, you can just check that the number is greater than 0 and you’re set.
Is there any situation where the isNaN check is needed? Those values will just just fail the condition when checked too, right?
In answer I don’t know. Haven’t really used isNaN that much.
Given that Number returns NaN when it fails, I just thought I’d experiment.
Number is obviously a simpler and clearer solution.
The only other thing to consider is when an empty string is involved, in which case Number converts that to 0. So you would need a separate check if 0 is a valid number too.