Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Jquery validate plugin for negative value checking on change of the input box

I have multiple input boxes and I have to check whether the entered values are positive or negative using jquery Validator plugin.

It is basically doing some calculation based on the value entered in the first input box.

The value entered must be positive, if not I should throw an error message saying the value must be positive or greater than zero.

like image 779
Java Questions Avatar asked Dec 06 '22 06:12

Java Questions


2 Answers

Use the min rule to specify that the minimum value is 1.

$("#formid").validate({
    rules: {
        fieldname: {
           min: 1
        },
    messages: {
       fieldname: {
           min: "Value must be greater than 0"
       }
    }
});
like image 117
Barmar Avatar answered Dec 08 '22 19:12

Barmar


You can use regex for validating numeric value. Code given below. The function will return true if input box contains numeric value (negative or positive)

function checkNumericValue(num)
{
var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
return objRegExp.test(num);
}

For checking -ve value, follow the link How to check the value given is a positive or negative integer?

like image 38
shairya Avatar answered Dec 08 '22 19:12

shairya