Capture the hash with location.hash
and check that it has a number with .test()
using a regular expression \d
(number from 0 to 9):
var hash = location.hash;
if(/\d/.test(hash)){
console.log("tem número");
}else{
console.log("não tem número");
}
You can use a function for this too:
function verHash(i){
if(/\d/.test(i)) return true;
return false;
}
verHash(location.hash);
Then you can assign the value of the function to a variable if you want and check:
var tem_numero = verHash(location.hash);
if(tem_numero){
console.log("tem número");
}else{
console.log("não tem número");
}
Or you can check directly on if
without declaring a variable if you want:
if(verHash(location.hash)){
console.log("tem número");
}else{
console.log("não tem número");
}
To verify that the hash contains only numbers (eg, # 123, # 1
etc.) change the regular expression from \d
to ^#\d+$
.