Regular expression voodoo

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have never managed to properly understand regular expressions, as far as I
am concerned they are some kind of gobldegook that performs magic voodoo.
However i have a TextBox that must be set to one of three values VS,WS or FP.
Right i thought even i can manage a regular expression that does that. I
created a RegularExpressionValidator, pointed it at the relevent control and
set the ValidationExpression to VS|WS|FP But it does not work. If is set it
to vs|ws|fp it correctly validates agains vs,fp or ws but the values have to
be in uppercase. What really simple and obvious thing am i missing.
 
You're not missing anything, Regular Expressions are case sensitive by
nature, and the RegularExpressionValidator doesn't support clientside
case insensitivity. You can add the modifier so you'd have
"(?i:vs|ws|fp) but that'll choke if you don't turn off the clientside
support.

In Javascript you want var myRegEx = /vs|ws|fp/i;
 
But i want it to be case sensitive, and it sort of is in that if i set the
ValidationExpression to vs|ws|fp then it will only allow me to enter vs, ws
or fp but not VS,WS or FP. But i want it to do the oposite allow VS, WS or
FP but not vs,ws or fp (or any other string). But if i set the
ValidationExpression to VS|WS|FP then fails validation whatever i do,
uppercase or lowercase.
 
It worked fine with me:

<asp:TextBox ID="txtBox1" Runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="valtxtBox1" Runat="server"
ValidationExpression="^VS|WS|FP$"
ErrorMessage="Invalid entries"
Display="Dynamic"
ControlToValidate="txtBox1">
</asp:RegularExpressionValidator>
 
Take the user input from the textbox and create a string i.e.

string _strToCheck = _tbInput.Text.ToLower();

Now run your Regular Expression server side. This way you can control
how the input is formatted.

Regex _strPattern = new Regex(@"="^vs|ws|fp$",
RegexOptions.IgnoreCase);
Match _matches = _strPattern.Match(_strToCheck);

string _patternMatch = _matches.ToString();

Don't forget to use the System.Text.RegularExpressions namespace.

This is how I would go about doing this. But to each his/her own....

- Will
 
Almost forgot, easier method!

Change the css attributes of your textbox so that by default input is
Upper-case.

text-transform: uppercase (i think)
 
Right, on further investiagtion it apears not to be a problem with the
validation expresion but a problem with actual validation taking place. The
textbox being validated is being filled with a value from a database, when
the page loads it apears as though the validation for this textbox has
already failed, in that the error message is displayed, however the value in
the textbox is valid. However if you do something to re-trigger validation
such as causing a postback then it validates OK. At this point it has failed
validation and then passed it without ever changing the value in the control
being validated. anyone any idea what is going on hear.
 
Back
Top