-1

How to check if the string contains a number in javascript without regular expressions ?

Daniel
  • 13
  • 2
  • Does this answer your question? [How to check whether a string contains a substring in JavaScript?](https://stackoverflow.com/questions/1789945/how-to-check-whether-a-string-contains-a-substring-in-javascript) – Nilanka Manoj Apr 26 '20 at 13:50
  • What is your reasoning for not wanting to use a regular expression? – charlietfl Apr 26 '20 at 13:56

2 Answers2

0

Try

str.indexOf(number.toString()) > -1
Luv
  • 386
  • 4
  • 15
-1

You can check as follows:

if (mystring.indexOf(21) >= 0){
    // Contains number 21
}
Lore
  • 34
  • 7