Skip to content

Instantly share code, notes, and snippets.

@danakt
Last active February 27, 2018 17:05
Show Gist options
  • Save danakt/5e48122cbffb9e1e077fcc8fbebe3493 to your computer and use it in GitHub Desktop.
Save danakt/5e48122cbffb9e1e077fcc8fbebe3493 to your computer and use it in GitHub Desktop.
Validate text input for numer
/**
* Checks text input for valid integer/float
* @param {string} value The input value
* @returns {boolean} Result of the validate
*
* @example
* validateInputForNumber('123') // true
* validateInputForNumber('123.123') // true
* validateInputForNumber('123.') // (!) still true
*
* validateInputForNumber('123.123.123') // false
* validateInputForNumber('g12redfso5tre45llm') // false
*/
export function validateInputForNumber(value) {
return /^([0-9]+\.?([0-9]+)?)$/.test(value)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment