How to check if string contains only digits in JavaScript ?
Given a string and the task is to check whether it contains only digits or not.
JavaScript test() Method: This method tests for a pattern in a string. This method returns true if match is found, otherwise it returns false.
Syntax:
RegExpObject.test(str)
Parameters: This function accepts single parameter str which is required. It specifies the string which to be searched.
Example 1: This example checks for the non-digit string by using RegExp.
<!DOCTYPE HTML> <html> <head> <title> Check if string contains only digits </title> </head> <body style = "text-align:center;" id = "body"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </p> <button onclick = "gfg_Run()"> check </button> <p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var path = "4323424242"; el_up.innerHTML = "String = '"+path + "'"; function gfg_Run() { el_down.innerHTML = /^\d+$/.test(path); } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 2: This example checks for the non-digits string by using RegExp.
<!DOCTYPE HTML> <html> <head> <title> Check if string contains only digits </title> </head> <body style = "text-align:center;" id = "body"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </p> <button onclick = "gfg_Run()"> check </button> <p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var path = "+4323424242"; el_up.innerHTML = "String = '"+path + "'"; function gfg_Run() { el_down.innerHTML = !/\D/.test(path); } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- JavaScript | Check if a string is a valid JSON string
- How to check if a string is html or not using JavaScript?
- JavaScript | Check if a variable is a string
- How to check a string is entirely made up of the same substring in JavaScript ?
- How to check empty/undefined/null string in JavaScript?
- JavaScript | Check if a string is a valid hex color representation
- JavaScript | Difference between String.slice and String.substring
- JavaScript | Insert a string at position X of another string
- How to count string occurrence in string using JavaScript?
- PHP | Program to check a string is a rotation of another string
- Substring check in JavaScript
- How to check if a variable is an array in JavaScript?
- How to check a function is defined in JavaScript ?
- How to check an object is empty using JavaScript?
- Check if an array is empty or not in JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



