How to validate valid characters for latitude and longitude during typing?

0

I have a text input for latitude typing and another for longitude.

I would like the field to allow typing only the characters valid for latitude and longitude.

I believe the field should only accept numbers, "." and "-", correct?

At first I think that something with regex works, but I do not know what regex would look like:

    // Campo para latitude e longitude;
    $(".latitude, .longitude").on('keypress', function (event) {
        var regex = new RegExp(??????);
        var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
        if (!regex.test(key)) {             
           event.preventDefault();             
           return false;
        }  
    }); 
    
asked by anonymous 28.03.2018 / 21:04

1 answer

1

Well basic but it worked ...

    $("[id*='" + camposSharepoint.Ocorrencias.LatitudeEvento + "'],[id*='" + camposSharepoint.Ocorrencias.LongitudeEvento + "']").on('keypress', function (event) {
        var regex = new RegExp("[0-9\-\,]");
        var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
        if (!regex.test(key)) {             
           event.preventDefault();             
           return false;
        }  
    }); 

If someone manages to improve so that they do not repeat the characters "-" and ","     

29.03.2018 / 15:11