How to validate a string with a Regular Expression?

  • Thread starter Thread starter Friso Wiskerke
  • Start date Start date
F

Friso Wiskerke

Hi All,

I'm trying to find a way to validate a string variable in the code behind
with a regular expression. I know there are validator controls but these all
work with the ControlToValidate property and I don't have a control, just a
string variable.

Can anyone help me out?

TIA,
Friso Wiskerke
 
Friso,

The validators as you describe it are in my opinion not code behind.

Can you tell something more what you want to achieve, because when you use
code behind you have to send information to that. And for that you use one
or the other control.

To say it in other words, somewhere should the information be typed in.

Cor
 
Cor,

The data that I'm trying to validate resides in a database. I writing code
that's validating the current data in the database, that's done by looping
through a datatable (or datareader) and check each row field by field.

When using a ValidatorControl you have to assign a control to it. Only
controls of type: TextBox, ListBox, DropDownList, RadioButtonList,
HtmlInputText, HtmlInputFile, HtmlSelect and HtmlTextArea can be assigned,
so I can't assign a DataColumn object or a string variable to it.

That's why I'm looking for a way to achieve some other kind regular
expression validation way in code-behind .

Cheers,
Friso
 
Finally found it searching through the namespaces !!

There's a function: System.Text.RegularExpressions.Regex.IsMatch() which
takes 2 parameters: "input" and "pattern". The first is the string to
validate, the seccond is the regular expression that the string is validated
against. The result is a boolean, True if it's a match.

Thanx,
Friso
 
Friso Wiskerke said:
I'm trying to find a way to validate a string variable in the code behind
with a regular expression. I know there are validator controls but these
all
work with the ControlToValidate property and I don't have a control, just
a
string variable.

Take a look at 'System.Text.RegularExpression.Regex' and its 'IsMatch'
method. If you want to get all matches, use 'Matches'.
 

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

Back
Top