Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JQUERY Validation Plugin bug -- rule of min(value) does not respect the minimum value, if the value contains commas #188

Closed
rchakravarti opened this issue Aug 25, 2011 · 9 comments

Comments

@rchakravarti
Copy link

Reference: http://docs.jquery.com/Plugins/Validation/Methods/min#value
Validation Plugin Version 1.8.1
jQuery Core Version 1.5.1

To reproduce the bug:
On the Demo for the Validate a minimum of 13,

  1. Enter the value 1,300 (1300 with a comma) with
    the "min" rule in place, validation fails

Any number with a comma fails validation when the
"min" rule is in place, the validate plugin incorrectly reports an error

  1. Remove the "min" requirement from the rule, and enter
    1,300 (with the comma) -- the error goes away

Expected behavior:
The plugin should treat numbers with commas in the same
manner as numbers without commas.


The same issue was encountered using the "range" rule

Appreciate any reply to help on this issue

@jzaefferer
Copy link
Collaborator

A proper solution would use Globalize (https://github.com/jquery/globalize) to handle the number parsing. Requiring that as a dependency isn't really an option, so we need to find a way to support it when you need it.

@ghost
Copy link

ghost commented Oct 1, 2011

I submit a solution (in French) :
http://www.mb-blog.fr/2011/09/30/validation-dun-champ-decimal-avec-virgule-ou-point-sous-jquery/

This post was inspired by this article : http://rebuildall.umbraworks.net/2011/03/02/jQuery_validate_and_the_comma_decimal_separator

I think that is a proper solution pending that this issue is fixed.

@rchakravarti
Copy link
Author

Mr. Arxuis --

Thank you very much for the reply to my query. We did as you suggested and it seems to work.

However, I would like to point out a small syntax issue with your code :

$.validator.methods.range = function (value, element, param) { return this.optional(element) || (safeStringToFloat(value) >= param[0] && globalizedValue }

There is a close parenthesis missing here -- the corrected line should be :

$.validator.methods.range = function (value, element, param) { return this.optional(element) || (safeStringToFloat(value) >= param[0] && globalizedValue); }


Once again, thanks for your input and help in this matter.
With regards,
RC

@ghost
Copy link

ghost commented Oct 3, 2011

Hi,

Endeed, I didn't seen that there was many errors in the script (my personal copy was good, though) I hava just submit the good version of the script.

Enjoy ;)

@mlynch
Copy link
Contributor

mlynch commented Apr 19, 2012

I recently submitted a PR For this issue with is the same as #379.

I agree with @jzaefferer, my solution doesn't seem that great now thinking about a better, internationalized number parsing function.

@jzaefferer
Copy link
Collaborator

I changed my mind on making Globalize a dependency, see #581. Individual work arounds don't scale.

@isatbox
Copy link

isatbox commented Jul 17, 2014

Hi all, i know its been a year ago this issue raised, but i manged with this trick:

first you initiate the amount field with thousand seperator and decimal seperator.
$('#form_amount_field').autoNumeric('init', {aSep: ' ', aDec: ','});

the you override the minimum validator method:

jQuery.validator.addMethod("min", function(value, element) {
var cents = parseFloat(value.replace(" ", "").replace(",", ".")).toFixed(2) * 100;
return this.optional(element) || (cents >= minimumLimit);
}, jQuery.validator.format("Can not enter amount lower than 5€"));

hope it helps :)

@Spongman
Copy link

this is a joke. both issues #188 and #379 which have been unfixed for years are closed referring to each other as reasons for closing. yet this fix STILL has not made it into release.

@joeRinehart
Copy link

In case anyone hits this and needs a quick, U.S.-but-customizable fix, I already had numeral.js involved and using it to preformat values was a pinch:

https://gist.github.com/joeRinehart/0e8af523fdfc567cc670ddca2eaff137

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants