[Javascript] How to check if a string contains only numbers?
I want to validate a input string contains only numbers, How to do that in Javascript?
2 Answers
3 years ago by Jahaan
The simplest way is by using regex. Check the below example to check if the given string contains only numbers.
let str1 = '12345';
let str2 = 'e45';
let regex = /^\d+$/;
console.log(regex.test(str1)); // outputs true
console.log(regex.test(str2)); // outputs false
3 years ago by Meera
We try to determine whether the string we are trying to check contains only entirely numeric characters using the regular expression / [0-9] + /. The character "/" serves as the expression's separator at both the beginning and finish, according to an analysis of this expression. It's crucial to be aware that other delimiters, such as (),, [], >, or #, can be used as start and end delimiters. For instance: # [0-9] + # See this link
https://kodlogs.net/192/javascript-check-if-string-contains-only-numbers
2 years ago by Sanjay Kapoor