简体   繁体   English

如何在javascript中分别验证纬度和经度?

[英]How to validate latitude and longitude separately in javascript?

if ((get.latitude).match(/((\d+)+(\.\d+))$/))
    Analytics_Latitude = get.latitude;
else
    Analytics_Latitude = 'undefined';

if ((get.longitude).match(/((\d+)+(\.\d+))$/))
    Analytics_Longitude = get.longitude;
else
    Analytics_Longitude = 'undefined';

I am getting latitude and longitude value from http://www.telize.com/geoip in my js file now I want to validate that value using javascript... Here I am getting this error like Uncaught TypeError: undefined is not a function 我现在从js文件中的http://www.telize.com/geoip获取纬度和经度值,现在我想使用javascript验证该值。在这里,我遇到了类似Uncaught TypeError的错误:undefined不是一个函数

Before trying to regex the value, you got to check if it exists. 在尝试对值进行正则表达式之前,您必须检查它是否存在。

You can do this this way: 您可以这样操作:

if(typeof get.latitude !== 'undefined'){...}

Second thing is that putting () between string and match seems to be wrong... 第二件事是将()放在字符串和匹配之间似乎是错误的...

Change this: 更改此:

(get.latitude).match(/((\d+)+(\.\d+))$/)

to this: 对此:

get.latitude.match(/((\d+)+(\.\d+))$/)

Third thing is that latitude is probably some float point number... so before acting as on string, you need to convert it: 第三件事是, latitude可能是一些浮点数...因此,在对字符串执行操作之前,您需要对其进行转换:

get.latitude.toString().match(...)

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

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