Mohamed Salem Lamiri
Mohamed Salem Lamiri

Reputation: 6077

jQuery Validate - Numbers with commas into specific range

I am using jQuery Validator to validate my form, however I need to be able to validate numbers with commas ex:

10,5
1,5

and into the range of :

[0, 60]

So until now I am only being able to validate numbers with commas, using this custom Regex :

       $.validator.addMethod("regex", function(value, element) {
            return this.optional(element) || /^(\d+|\d+,\d{1,2})$/i.test(value);
        }, "Number is invalid: Please enter a valid number.");


       $('.my-form').validate({
            rules: {
                "my-field-input": {
                    required: true,
                    regex: "Required number"
                }
            }
        });

So at the end I would like that user can only input numbers with commas and into range of 0 .. 60 ex:

10,5
1,5
60
50,5

And step is : 0,5

Any suggestion ? Should I keep doing it with jQuery Validator or create some custom code ? thanks!

Upvotes: 1

Views: 1469

Answers (2)

Zorken17
Zorken17

Reputation: 1896

Otherwise you almoset hade it

^(\d{1}|[0-5]{1}\d{1}|5[0-9]+,\d{1,2}|60)$

Upvotes: 0

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34152

Make sure that the number is is one digit number or if it is two digit the first one is less than 6, or it is equal to 60

^([0-9]{1}|(([0-5]{1}[0-9]?))(,\d{1})?|60)$

here is the fiddle: https://regex101.com/r/cW3rB7/4

Upvotes: 1

Related Questions