Click here to Skip to main content
15,891,694 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im Looking to validate longtude and latitude input fields to ensure correct format is inputed

Inputs that must match
27⁰33,04.24'' S 28⁰50,24.18'' E


What I have tried:

Regex regex = new Regex(@"^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$");


           if (!regex.IsMatch(latitude))
           {
               JQGrid1.ShowEditValidationMessage("Ivalid latitude");
           }
Posted
Updated 6-Aug-20 23:46pm
v2
Comments
Patrice T 7-Aug-20 4:18am    
What is the problem with this code ?
Member 14370529 7-Aug-20 4:33am    
when regex.IsMatch(latitude) == true
it still says it is invalid
Patrice T 7-Aug-20 4:41am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

How do you know that it matched ?
Show sample inputs that should match

1 solution

Quote:
when regex.IsMatch(latitude) == true
it still says it is invalid

Because your input and regex do not match.
Your input format look weird:
Instead of
27⁰33,04.24'' S 28⁰50,24.18'' E
it should look like
27⁰33'04.24'' S 28⁰50'24.18'' E
or
27⁰33'04.24" S 28⁰50'24.18" E
and also
27⁰ S 28⁰ E
27⁰33' S 28⁰50' E
27⁰33'04" S 28⁰50'24" E
should be valid

In your regex, I don't see any place to match °, ', ", B or E
Your regex is actually matching
27.45, 28.50


You should start by listing all valid input formats, then build the regex.

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
 
Share this answer
 
v3
Comments
Member 14370529 7-Aug-20 6:07am    
Thanks
[no name] 9-Aug-20 13:10pm    
That is a cool one for a 5
Patrice T 9-Aug-20 13:14pm    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900