jQuery Validation Plugin: validate decimal number with comma as decimal separator

Nuno Nogueira

HTML:

<form id="myform">
    <label for="field">Required, decimal number:</label>
    <input class="left" id="field" name="field">
    <br/>
    <input type="submit" value="Validate!">
</form>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://jquery.bassistance.de/validate/jquery.validate.js"></script>
<script src="http://jquery.bassistance.de/validate/additional-methods.js"></script>
<script>

JS:

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

This is requiring a valid number in field "field". However, when using comma as decimal separator, it doesn't work. Eg: 10,25 is not considered a valid number.

I took this example from the validation plugin You may also see a live JSFiddle here.

I guess I will have to convert the input (from comma to dot decimal separator) before it is validated, but how?

Arun P Johny

You can use the pattern rule to pass a custom regex pattern like

$("#myform").validate({
    //for debug only
    debug: true,
    rules: {
        field: {
            required: true,
            pattern: /^(\d+|\d+,\d{1,2})$/
        }
    },
    messages: {
        field: {
            pattern: 'Please use the proper pattern'
        }
    }
});

Demo: Fiddle


If it is a repeated pattern create a custom validation rule

jQuery.validator.addMethod("mynumber", function (value, element) {
    return this.optional(element) || /^(\d+|\d+,\d{1,2})$/.test(value);
}, "Please specify the correct number format");

$("#myform").validate({
    //for debug only
    debug: true,
    rules: {
        field: {
            required: true,
            mynumber: true
        }
    }
});

Demo: Fiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

MVC/JQuery validation does not accept comma as decimal separator

formatting decimal number to string with comma as decimal separator in racket

decimal separator in woocommerce to be a comma

Convert String with Dot or Comma as decimal separator to number in JavaScript

AngularJS: Change decimal separator to comma

Replace point decimal separator with comma

Format a decimal with comma as decimal separator and without thousands separator

Regex a decimal number with comma

decimal number comma styling

Is there any option to store a number as text with a specific format (comma as thousands separator and period as decimal separator) in Snowlake?

regex for decimal number validation

Parsing numbers with a comma decimal separator in JavaScript

Best way to parseDouble with comma as decimal separator?

regex expression to match decimal numbers with comma as a separator

Show comma instead of point as decimal separator

Only add thousand separator before decimal comma

Thousand separator while typing and decimal (with comma)

Add comma separator to a numbers with 2 decimal points

NumericUpDown: accept both comma and dot as decimal separator

How to write a csv with a comma as the decimal separator?

Replace comma with decimal separator from TextField

Xamarin Forms Entry: Use comma as decimal separator

seq uses both comma and dot as a decimal separator

Read txt file with comma decimal separator in MATLAB

Convert a double to 2 decimal places with comma separator

bc: decimal separator comma vs. point

Google form regex for numbers with comma as decimal separator

Pyspark, how to write a df with comma as a decimal separator

Use pandas style using comma as decimal separator