javascript check if string contains only numbers code example
Example 1: check if string only contains integer digits numbers javascript
let val = '1234567890'
let isnum = /^\d+$/.test(val);
if (isnum) {
}
Example 2: javascript check if string includes numbers
function containsNumbers(str){
var regexp = /\d/g;
return regexp.test(str);
};
containsNumbers("Here are some Numbers 2123");
containsNumbers("Here are no Numbers");
Example 3: how to check if a string contains numbers in javascript
function hasNumbers(str){
var regexp = /d/g;
return regexp.test(str);
};
hasNumbers("I am 20years old");
hasNumbers("I am a boy");