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

Asked

Viewed 322 times

0

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

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

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

At first I think something with regex works, but I don’t know how the regex:

    // 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;
        }  
    }); 

1 answer

1


Pretty 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 can improve so that they don’t repeat the characters "-" and "," it would be perfect

  • Pass a list of examples... a mcve of an Ideone or something like... Examples of error, hits, etc. Or Regex101

Browser other questions tagged

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