How can we check if the input number is either positive or negative in LiveValidation?
Asked
Active
Viewed 2.3k times
3 Answers
5
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
- 40,053
- 20
- 133
- 188
-
regular expression for checking positive number – Nazmul Hasan Jul 04 '09 at 08:40
4
try
{
if ((new Number( $('#numberInput').val()) < 0)
{
// Number is negative
}
else
{
// Otherwise positive
}
} catch ( error)
{
alert( "Not a number!");
}

Artem Barger
- 40,769
- 9
- 59
- 81
-1
You can also use JavaScript's method eg:
var pos_value = Math.abs(n_val);
Thanks Dev

Dev
- 7
- 1
-
1The question is finding negative number, not converting to positive numner. – prakashstar42 Dec 09 '14 at 09:01