Validate string format "Latitude, Longitude"

0

I have an entry type user control, and I would like the user to only be able to enter the following format:

Latitude, Length as for example "12.2323, -1.53452"

That is, you can only enter numbers, periods, 1 comma, and the minus sign. On the other hand I read that the latitude must be a number between -90 and 90 and the length between -180 and 180

How can I do this? With what functions can I support myself to carry out the validation? Thanks

    
asked by Alfredo Lopez Rodes 17.08.2018 в 17:54
source

1 answer

1

You can use a regular expression:

import re
patron = re.compile('^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$') #patrón que debe cumplir
if (not patron.match(latitud) is None) and (not patron.math(longitud) is None):
    #Código a ejecutar si son coordenadas válidas
else:
    #Código a ejecutar si las coordenadas no son válidas

With import re we import the regular expression library.

re.compile() Compile a regular expression.

patron.match() Returns None if the variable does not follow the regular expression.

latitude and length are the variables where you have stored their respective values.

I have not added the check of whether the longitude and latitude are between -90 and 90, it's something trivial, I'm sure you can do it alone:)

    
answered by 17.08.2018 / 19:34
source