Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking for positive or negative number

How can we check if the input number is either positive or negative in LiveValidation?

like image 930
Nazmul Hasan Avatar asked Mar 01 '23 13:03

Nazmul Hasan


1 Answers

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)
 }
like image 154
TheVillageIdiot Avatar answered Mar 13 '23 05:03

TheVillageIdiot