1

trying to sort out a regular expression for the following string:

51.4920302, -0.0850667

So far I have:@"^[0-9]*,{-}[0-9]*$" but it doesn't seem to be working.

Any thought, greatly received.

The whole snippet is:

[RegularExpression(@"^[0-9]*,{-}[0-9]*$", ErrorMessage = "Must enter a valid coordinate")]
    public string FaveRunLatLng2 { get; set; }

Thanks.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
sald
  • 21
  • 1
  • 4

2 Answers2

2

You're not allowing for the decimal point. You're also basically requiring the second part of the coordinates to be negative and not allowing the first. Try

@"^-?[0-9]+\.[0-9]+, -?[0-9]+\.[0-9]+$"
BishopRook
  • 1,240
  • 6
  • 11
0
\b-?\d+\.\d+, -?\d+\.\d+\b

If you want the space to be optional, you can add \s?, like this:

\b-?\d+\.\d+,\s?-?\d+\.\d+\b

As long as you know your input is going to have a comma and space. If it is entered by a user, you may need to sanitize it first.

Here is a tester online you can use: http://www.regular-expressions.info/javascriptexample.html

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
Furbeenator
  • 8,106
  • 4
  • 46
  • 54