views:

31

answers:

2

I have this C# regex:

^\s?((?<qty>\d+)\s?/)?\s?[$]?\s?(?<price>\d{0,2}(?:\.\d{1,2})?)\s?$

and use MVC's data validation at the client. JavaScript says this regex is invalid, although C# works perfectly fine. Any idea how to get it to work for both C# and JavaScript, since it doesn't seem possible to provide a separate JavaScript Regex in the Data Validation annotations?

The regex validates a quantity and price. 4/$2.69 for example.

+2  A: 

Javascript does not support named reference (?<…>…). You need to use

^\s?((\d+)\s?/)?\s?[$]?\s?(\d{0,2}(?:\.\d{1,2})?)\s?$

and refer qty and price as 1 and 2 instead.

KennyTM
Very nice catch!
Dr. Zim
+1  A: 

Remove the group names (<qty>).

SLaks