how we can check the input number either positive or negative in live validation???
A:
try
{
if ((new Number( $('#numberInput').val()) < 0)
{
// Number is negative
}
else
{
// Otherwise positive
}
} catch ( error)
{
alert( "Not a number!");
}
Artem Barger
2009-07-04 08:10:27
+1
A:
easier way is to multiply the contents with 1 and then compare with 0 for +ve or -ve
try{
var n=$("#...").val() * 1;
if(n>=0){
//...Do stuff for +ve num
}else{
///...Do stuff -ve num
}
}catch(e){
//......
}
REGEX:
var n=$("#...").val()*1;
if (n.match(new RegExp(^\d*\.{0,1}\d*$))) {
// +ve numbers (with decimal point like 2.3)
} else if(n.match(new RegExp(^-\d*\.{0,1}\d*$))){
// -ve numbers (with decimal point like -5.34)
}
TheVillageIdiot
2009-07-04 08:21:25
regular expression for checking positive number
nazmul hasan
2009-07-04 08:40:14