简体   繁体   中英

How to validate latitude and longitude and e-mail addresses in jQuery

I am creating a validation of latitude and longitude.

Right now this is my jQuery script:

var latlngVal = /^(-?([1-8]?[1-9]|[1-9]0)\.{1}\d{1,6}),{1}(-?([1]?[1-7][1-9]|[1]?[1-8][0]|[1-9]?[0-9])\.{1}\d{1,6})/;
var latlng = $("input#latlng").val();
var invalid_latlng = 'Latitude and Longitude are not correctly typed';

// Validate Latitude and Longitude
if(!latlngVal.test(latlng)) {

    // ERROR

    $("#error").html(invalid_latlng);
    return false;;
};

The output from Google can be like this: 63.548552, -127.529297.

I am showing them how to copy the latitude and longitude directly from Google and put it in with a video. But somehow, the validation continues to say the latitude and longitude typed is wrong.

I would like to know if my var latlngVal is correct or wrong.

And then I am doing this script also, and it doesn't seem to work either..:

var emailVal = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
var email = $("input#email").val();
var invalid_email = 'Please type a valid e-mail address';

// If e-mail address is not empty - validate
if (!email == '') {
    if(!emailVal.test(email)) {

        // ERROR - customize

        $('#error').html(invalid_email);
        return false;
    }
};

The problem here, is just that it doesn't seem to check if the email is empty, so please tell me if something here also is wrong. I don't want to validate the email if they didn't type one.

I took the liberty of making some changes in your code.

I changed your RegEx to:

/^-?([0-8]?[0-9]|90)\.[0-9]{1,6},-?((1?[0-7]?|[0-9]?)[0-9]|180)\.[0-9]{1,6}$/;

It valids lat -90.XXXXXX to 90.XXXXXX and lng -180.XXXXXX to 180.XXXXXX

Example: -85.123456,-100.123456

And to check if email is empty:

if (email.length !== 0) {

DEMO http://jsfiddle.net/nRha8/2/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM