Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression Validators

Status
Not open for further replies.

Linda224

Programmer
Dec 6, 2006
80
US
Hello
I have a c#.net application that I am working on putting some validation on the form.
I have 9 regular expression validators on one page and I want to know if it is possible that only one will show up at a time.
Right now if they fail two or more of the validators they all show up but I would like only one to show up and then when they correct that error the next one show up.

Is that possible?
Thank you for any help
Linda
 
I think you would have to use server side code to do it - which would mean a round trip to the server each time a control is validated.
 
are you talking about webforms validation controls? if so you may want to post in forum855.

if you you want 1 regular expression to fail at a time then you will either need a rather nasty expression, or custom code. with the custom code you could load the expressions into any array and take the first failing expression.
Code:
var patterns = new[]
{
  new 
    {
       pattern = new Regex("pattern 1"),
       message = "Error Message 1"
    },
  new 
    {
       pattern = new Regex("pattern 2"),
       message = "Error Message 2"
    },
  new 
    {
       pattern = new Regex("pattern 3"),
       message = "Error Message 3"
    },
};
var failed = patterns.FirstOrDefault(x => x.pattern.IsMatch(value) == false);
if(fail != null)
{
   args.IsValid = false;
   args.ErrorMessage = failed.message;
}
else
{
   args.IsValid = true;
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top