简体   繁体   English

如何在jQuery中验证经度和纬度以及电子邮件地址

[英]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: 现在这是我的jQuery脚本:

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. Google的输出可以是这样的: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. 我想知道我的var latlngVal是正确还是错误。

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: 我将您的RegEx更改为:

/^-?([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 它将lat -90.XXXXXX转为90.XXXXXX ,将lng -180.XXXXXX转为180.XXXXXX

Example: -85.123456,-100.123456 示例: -85.123456,-100.123456

And to check if email is empty: 并检查电子邮件是否为空:

if (email.length !== 0) {

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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