black_belt
black_belt

Reputation: 6799

Validating Decimal Numbers

I have a form and I am using jQuery validation plugin to validate it. Now I am trying to validate decimal number input.

I have tried the following code, but its not working. Is the problem with the regular expression or the way of writing the custom rule is wrong in my code?

 rules: {
      paid_amount: {
              required:true,
                      rexp: ^[1-9]\d*(\.\d+)?$
      },

   }
     },

 messages: {
             paid_amount: {
                      required: "Enter Paid Amount",
              rexp:"Decimal Numbers Only"
    },  

     }
});

Upvotes: 12

Views: 65034

Answers (5)

merlincita_pj
merlincita_pj

Reputation: 1

Your problem is with the method 'rexg'. You should use 'pattern'. Example:

paid_amount: {
     required:true,
     pattern: ^[1-9]\d*(\.\d+)?$
}

Upvotes: 0

RN Kushwaha
RN Kushwaha

Reputation: 2136

jQuery validator also supports decimal validation using range method.

factor:
      {
        required: true,
        range:[0.08,20.09]  
      },

Read more here and demo here.

Upvotes: 3

user1263800
user1263800

Reputation:

I'm not familiar with jquery validator plugin, sorry but your regular expression is valid.

another approach would be

var reg = "/^[1-9]\d*(\.\d+)?$/"; //reg pattern<br/>
if(reg.test("1.23"))//validate
{
 //do something <br/>
}

Upvotes: 2

dSquared
dSquared

Reputation: 9825

You can use the number validation method found here

From the docs:

$("#myform").validate({
  rules: {
    field: {
      required: true,
      number: true
    }
  }
});

In your case:

rules: {
    paid_amount: {
        required:true,
        number: true
    }
},
messages: {
    paid_amount: {
        required: "Enter Paid Amount",
        number: "Decimal Numbers Only"
    }
}

Upvotes: 18

Robert Niestroj
Robert Niestroj

Reputation: 16131

Use the jQuery isNumeric function: http://api.jquery.com/jQuery.isNumeric/

Upvotes: 0

Related Questions