Quantcast
Viewing all articles
Browse latest Browse all 14

Regex for Numbers Only JavaScript

hash = window.location.hash.substr(1);
var reg = new RegExp('^[0-9]$');
console.log(reg.test(hash));

I get false on both “123” and “123f”. I would like to check if the hash only contains numbers. Did I miss something?

Solution

var reg = /^\d+$/;

should do it. The original matches anything that consists of exactly one digit.

The post Regex for Numbers Only JavaScript appeared first on Solved.


Viewing all articles
Browse latest Browse all 14

Trending Articles