简体   繁体   English

正则表达式验证纬度和经度的输入格式

[英]Regex Expression to validate input format of Latitude and Longitude

I need to validate the just the format of the entered string for Latitude and Longitude and not if its a correct value for Latitude or Longitude. 我需要验证纬度和经度输入字符串的格式,而不是纬度或经度的正确值。

The format Latitude 89:90:00N or 67:90:76 S and for Longitude 67:23:00E or 78:23:45W 格式纬度89:90:00N或67:90:76 S和经度67:23:00E或78:23:45W

I am using the below expression for which I am getting a false 我使用下面的表达式,我得到了一个假的

 if (!Regex.IsMatch(currentValue, "^[0-9][0-9]:[0-9][0-9]:[0-9][0-9][N][S]$"))

                                Errors.Text = "Invalid format of Latitude;

Please correct me where am I going wrong..I need to validate if its either N or S (without case sensitivity). 请纠正我在哪里出错。我需要验证它是N还是S(没有区分大小写)。

You have made both N and S mandatory. 你已经强制要求NS

"^(?:90|[0-8][0-9]):[0-5][0-9]:[0-5][0-9][NS]$"    // latitude
"^(?:180|1[0-7][0-9]|[0-9][0-9]):[0-5][0-9]:[0-5][0-9][EW]$"  // longitude

should work. 应该管用。 This will also reject invalid entries like 190:67:75E or 99:99:99S . 这也将拒绝190:67:75E99:99:99S等无效条目。

/^([0-9]{1,2}:[0-9]{2}:[0-9]{2}\s?[NS]|[0-9]{1,3}:[0-9]{2}:[0-9]{2}\s?[EW])$/i

should work 应该管用

Credit to Tim Pietzcker for the ^$ . 感谢Tim Pietzcker的^$ I forgot to put it in mine at the first :P 我忘了把它放在我的第一个:P

There's an error in N/S part. N / S部分有错误。 As well, you can make it more readable like that: 同样,你可以使它更具可读性:

if (!Regex.IsMatch(currentValue, "^\d{2}:\d{2}:\d{2}[SN]$"))

                            Errors.Text = "Invalid format of Latitude;

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

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