Conditional Regular Expression testing of a CSV
Posted
by Alastair Pitts
on Stack Overflow
See other posts from Stack Overflow
or by Alastair Pitts
Published on 2010-04-14T06:17:32Z
Indexed on
2010/04/14
6:23 UTC
Read the original article
Hit count: 615
I am doing some client side validation in ASP.NET MVC and I found myself trying to do conditional validation on a set of items (ie, if the checkbox is checked then validate and visa versa). This was problematic, to say the least.
To get around this, I figured that I could "cheat" by having a hidden element that would contain all of the information for each set, thus the idea of a CSV string containing this information.
I already use a custom [HiddenRequired]
attribute to validate if the hidden input contains a value, with success, but I thought as I will need to validate each piece of data in the csv, that a regular expression would solve this.
My regular expression work is extremely weak and after a good 2 hours I've almost given up.
This is an example of the csv string:
true,3,24,over,0.5
to explain:
- true denotes if I should validate the rest. I need to conditionally switch in the regex using this
- 3 and 24 are integers and will only ever fall in the range 0-24.
- over is a string and will either be
over
orunder
- 0.5 is a decimal value, of unknown precision.
In the validation, all values should be present and at least of the correct type
Is there someone who can either provide such a regex or at least provide some hints, i'm really stuck!
© Stack Overflow or respective owner