Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

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 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 commented Apr 5, 2016

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

@xhh2a
Copy link

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 commented Apr 9, 2016

This option is optional by default

@mondwan
Copy link
Contributor Author

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 as completed in 578426d Apr 17, 2016
@ansman
Copy link
Owner

ansman commented May 20, 2016

This is now released in 0.10.0

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

No branches or pull requests

3 participants