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

Feature request: Validate leading zero for numericality #106

Closed
mondwan opened this issue Mar 24, 2016 · 5 comments
Closed

Feature request: Validate leading zero for numericality #106

mondwan opened this issue Mar 24, 2016 · 5 comments

Comments

@mondwan
Copy link
Contributor

@mondwan mondwan commented Mar 24, 2016

Goal:

  • Expect "010" to be false in numericality validator

Description:

It will be great if there is an option like noLeadingZero in numericality.

Why

  • Value from HTML5 number box is string
  • The coercion of + will force "010" to be 10

Facts above make 010 to be a valid number which it should not be in reality in my opinion.

JS:

var c = {
    "numericality": {
    "greaterThanOrEqualTo":1,
    "lessThanOrEqualTo":100,
    "message":"<id> from 1 to 100",
  },
  "presence": true,
};

// Expect to be false, but get true
console.log(
    'validation result ',
    validate.single('010', c)
);

// No problem
console.log(
    'validation result ',
    validate.single('10', c)
);

Fiddler:

https://jsfiddle.net/8okbtbuz/2/

mondwan added a commit to mondwan/validate.js that referenced this issue Mar 24, 2016
@ansman
Copy link
Owner

@ansman ansman commented Apr 5, 2016

Agreed, though I think the option should be called strict instead.

@xhh2a
Copy link

@xhh2a xhh2a commented Apr 9, 2016

Please make this an option, I would like cases where it is the current behavior.

@mondwan
Copy link
Contributor Author

@mondwan mondwan commented Apr 9, 2016

This option is optional by default

@mondwan
Copy link
Contributor Author

@mondwan mondwan commented Apr 9, 2016

By the way, option has been renamed to strict as requested in the pull request. However, I am not sure how to deal with floating point number. Should I allow all trailing zero if that is a float?

If yes, I can simply add a check and bypass this checking.

@ansman ansman closed this in 578426d Apr 17, 2016
@ansman
Copy link
Owner

@ansman ansman commented May 20, 2016

This is now released in 0.10.0

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

Successfully merging a pull request may close this issue.

None yet
3 participants