Regular expression voodoo

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.
 
F

Flinky Wisty Pomm

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;
 
G

Guest

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.
 
G

Guest

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>
 
P

Paul Bush

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
 
P

Paul Bush

Almost forgot, easier method!

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

text-transform: uppercase (i think)
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top