Capture the hash with location.hash
and check if you have 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 that 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 desired:
if(verHash(location.hash)){
console.log("tem número");
}else{
console.log("não tem número");
}
To check if the hash contains only numbers (ex. #123, #1
etc.) change the regular expression of \d
for ^#\d+$
.
Just one question, "any number" would be whether there is any number or should be exactly one valid number? Example:
#123abc
contains any number, would that be valid? Or should it be specifically a number, like#123
and#123a
would be invalid?– fernandosavio
@fernandosavio would only be numbers!
– MFT
MFT, could you clarify your question in sam’s reply comments ??
– fernandosavio
@clear fernandosavio, posted there
– MFT
Thanks, now it is well documented in case someone falls here in the future. D
– fernandosavio