? This project has moved to https://gitlab.com/ynote_hk/is-string-a-number.
Returns
true
if the value is a string number written in fixed notation and in decimal base.
This utility module has a front-end purpose. As JavaScript doesn't provide a clean and unique way to check if a value is a number or written in a specific notation or base, type checking on numbers is hard.
Front-end developers often have to deal with user inputs, especially within
forms. The need, here, is to validate a string
value as a simple number in
decimal base and fixed notation.
import isStringANumber from 'is-string-a-number'
isStringANumber('42') // => true
isStringANumber('42e42') // => false
isStringANumber('0x2a') // => false
isStringANumber('42.') // => false
isStringANumber(42) // => false
More details on the usage section.
Install with npm:
$ npm install --save is-string-a-number
With CommonJs:
const isStringANumber = require('is-string-a-number')
With ECMAScript module:
import isStringANumber from 'is-string-a-number'
isStringANumber('0') // => true
isStringANumber('42') // => true
isStringANumber('42.2') // => true
isStringANumber('-42') // => true
isStringANumber('-42.2') // => true
isStringANumber('-0.42') // => true
isStringANumber(0) // => false
isStringANumber(.42) // => false
isStringANumber(42) // => false
isStringANumber(-42) // => false
isStringANumber(-42.2) // => false
isStringANumber('+42') // => false
isStringANumber('42,2') // => false
isStringANumber('42e24') // => false
isStringANumber('42E24') // => false
isStringANumber('42e+24') // => false
isStringANumber('42e-24') // => false
isStringANumber('42foobar') // => false
isStringANumber('0x2a') // => false
isStringANumber('.') // => false
isStringANumber('42.') // => false
isStringANumber(null) // => false
isStringANumber(undefined) // => false
isStringANumber(NaN) // => false
isStringANumber('Alice in Wonderland') // => false
isStringANumber('') // => false
isStringANumber(' ') // => false
isStringANumber('\r\n\t') // => false
isStringANumber([]) // => false
isStringANumber({}) // => false
isStringANumber(function() {}) // => false
isStringANumber(true) // => false
isStringANumber(false) // => false
isStringANumber('true') // => false
isStringANumber('false') // => false
isStringANumber(Infinity) // => false
isStringANumber(-Infinity) // => false
isStringANumber(new Date()) // => false
Bug reports and pull requests are welcome on GitHub at https://github.com/Ynote/is-string-a-number. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
Everyone interacting in the is-string-a-number project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
Special thanks to Jon Schlinkert for his great is-number module that inspired me.
- The global parseFloat top-level function
- Displaying numbers in JavaScript by Axel Rauschmayer
- How numbers are encoded in JavaScript by Axel Rauschmayer
The module is available as open source under the terms of the MIT License.
MIT © Ynote_hk