Check whether an input string contains a number in javascript

Udara S.S Liyanage picture Udara S.S Liyanage · Apr 25, 2011 · Viewed 288.3k times · Source

My end goal is to validate an input field. The input may be either alphabetic or numeric.

Answer

Zon picture Zon · Mar 2, 2015

If I'm not mistaken, the question requires "contains number", not "is number". So:

function hasNumber(myString) {
  return /\d/.test(myString);
}