ASP.NET MVC 2 Data Validation: Make C# Regex work for both C# and JavaScript simultaneously?
I have this C# regex:
^\s?((?<qty>\d+)\s?/)?\s开发者_如何学C?[$]?\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.
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.
Remove the group names (<qty>
).
精彩评论