Anto
Anto

Reputation: 419

Validate Coordinate entry using RegEx

I was wondering if anybody could help with some RegEx validating. I have a text box where a user is to input a set of XY coordinates e.g. 123.345, 543.123 I can use the following bit of RegEx to check for a single X or Y coordinate. var pattern = "^[0-9]+[.]?[0-9]*$"; Allows only numbers and 1 decimal point to be entered.

But I can't figure out how to allow the user to enter a single whitespace and/or a comma after the X coordinate and then continue to enter the Y coordinate.

I know this would be easier with two different textboxes but because of the application and UI requirements I cannot add a second text box for the Y coordinate.

Thanks for any help!

Upvotes: 0

Views: 245

Answers (1)

Stephan
Stephan

Reputation: 43033

Try this :

^\d+(?:.\d+)?,\s\d+(?:.\d+)$

Explanation :

(1)    ^                 => Beginning of input
(2)    \d+(?:\.\d+)?     => Allow one or more digits followed optionally by a dot and one or more digits
(3)    ,\s               => Expect a comma and a single whitespace
(4)    \d+(?:\.\d+)      => See (1)
(5)    $                 => End of input

Nota : If you expect only a single space change ,\s into ,[ ].

Upvotes: 1

Related Questions