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

Add noSymbols option to isNumeric #848

Merged
merged 3 commits into from
Jul 31, 2018
Merged

Conversation

Maxattax97
Copy link
Contributor

Split #847 per request of @profnandaa.

@chriso
Copy link
Collaborator

chriso commented Jun 16, 2018

This is virtually the same as isInt() except it:

  • disallows negative numbers
  • allows empty strings (?)

Have you tried validator.isInt(str, {min:0})?

@Maxattax97
Copy link
Contributor Author

Maxattax97 commented Jun 16, 2018

My specific use case for this was TOTP two factor authentication codes. I didn't realize that isInt() had an option for leading zeroes (000000 is possible), so this may have been a better function to modify.

However, TOTP codes cannot contain any signs/symbols. One example that is possible with the isInt() regex is -000000, which is not a valid code.

Empty strings are also possible, but I figured it would be best to require the developer to use the already existing isByteLength() (or other) function to do this. Maybe this is a bad assumption?

@chriso
Copy link
Collaborator

chriso commented Jul 31, 2018

Thanks for the PR 😄 I'm going to change it to snake case (no_symbols) for consistency with the remainder of the lib.

@chriso chriso merged commit 0f4877b into validatorjs:master Jul 31, 2018
chriso added a commit that referenced this pull request Jul 31, 2018
@razakhawaja
Copy link

razakhawaja commented May 9, 2019

if ((Validator.isNumeric(num, [{no_symbols: true}])) === false) { errors.number = 'Please enter a valid number, e.g 03001234567'

This accepts strings with "+" and "." etc, even when no_symbols is true. Is there a syntax mistake I'm making or is it a bug?

@profnandaa
Copy link
Member

@razakhawaja - can give an example of the num you are looking at?

@razakhawaja
Copy link

An example was "+33333333333".
However, I found my mistake. The no_symbols object wasn't supposed to be enclosed in array brackets. But the syntax from the official documentation suggested a JSON within an array - would appreciate if the documentation provided examples of each use case as well!

@fonziemedia
Copy link

This seems to be broken in current versions.
Example:
isNumeric("0000000-000"); // returns false

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

Successfully merging this pull request may close these issues.

5 participants