Check if URL contains number

Asked

Viewed 151 times

6

I would like to check if the current URL contains numbers after the #.

I don’t need to know if it contains a specific number or a quantity of numbers, I just need to know if there is any number after #.

EX:

https://answall.com#123 TRUE

https://answall.com# FALSE

How could I do that?

  • 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 would only be numbers!

  • MFT, could you clarify your question in sam’s reply comments ??

  • @clear fernandosavio, posted there

  • 1

    Thanks, now it is well documented in case someone falls here in the future. D

3 answers

10


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+$.

  • Sam, AP clarified a little more in the question comment. As your answer is accepted and the most complete thought you could change the code to contemplate what he said now that it’s clearer what he needs.

  • Man, I was thinking, your answer allows #123abc as valid and does not match what AP wants. It would only use regex /^#\d+$/ instead of /\d/ that would be correct.

  • When I asked if it is to test if the hash contains numbers or if it is to test if it has only numbers, AP answered that would just be numbers. Hence my comment

  • I also found ambiguous, so I asked him. I’ll ask him to clarify this and if not, patience. D

  • Face, analyzing better, really you’re right, got a little ambiguous. I’ll add in the answer the check only with numbers.

  • In reality I would say it is indifferent because it is only two options, either it will have numbers after # or it will be empty.

Show 1 more comment

3

You can use the location.hash to make that check

if( ! isNaN( location.hash.substr(1) *1 ) )
{
    /// é um numero
}

3

A very simple example:

var valor1 = "https://answall.com#";
var valor2 = "https://answall.com#123i";

var verifica1 = valor1.substr(valor1.indexOf("#") + 1);
var verifica2 = valor2.substr(valor2.indexOf("#") + 1);

var encontrou1 = verifica1.match(/\d+/g);
var encontrou2 = verifica2.match(/\d+/g);

alert(encontrou1 != null);
alert(encontrou2 != null);

https://jsfiddle.net/9Lagsybt/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.