Validate google api key

3

I'm making an application, where I'll have to validate a google maps API key. The problem is that I do not know what patterns it follows:

AIzaSyA-OlPHUh2W7LUZXVjQjjxQ8fdCWdAASmc 

Here you can see that they are uppercase and lowercase, numbers, and dashes. But will it always be like this?

Does anyone know the pattern of these keys, and how to do this with JS and PHP?

JS:

var inputstr="A3aa622-_";
var regex=/^[A-Za-z0-9-_]{30,50}$/g;
if (regex.test(inputstr)){
    alert("sim");
}else{
    alert("Nao");
}
    
asked by anonymous 20.07.2016 / 15:09

1 answer

1

Assuming the key has exactly 39 size characters; Be formed by alphanumeric characters (letters and numbers), in any case (uppercase or lowercase); And it can display - and underlines _ traits.

Here are the codes in JS and PHP able to do this pre-validation *:

Code in JavaScript :

function is_valid_key( key )
{
    if( key.length != 39 ) return false;
    var regx = new RegExp("^[A-Za-z0-9-_]+$");
    return regx.test(key);
}

Code in PHP :

function is_valid_key( $key )
{
    if( strlen( $key ) != 39 ) return false;
    return preg_match( '/^[a-zA-Z0-9-_]+$/', $key );
}
  • The user @Inkeliz is right, the only way to validate the key is through a GET / POST in the Google API, so the above code is only a pre-validation in order to avoid a overhead unnecessary GETs and POSTs on the network.

I hope I have helped!

    
01.09.2016 / 20:33