Validation with validation block

92 Views Asked by At

I need to write a validation with MS enterprise library. The validation purpose is to validate a string that should have @ keyword at atleast once and it can have any thing of 120 length, now sure how to do it.

  <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.ContainsCharactersValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                 characterSet="@" containsCharacter="Any" name="Contains Characters Validator" />

trying something like this but doesn't solve the purpose.

or a regex will work..?

1

There are 1 best solutions below

4
Richard Ev On BEST ANSWER

Instead of using the ContainsCharactersValidator, you can use the RegexValidator with a suitable regular expression.

If your requirements are:

  • @ anywhere in the string, 0 to many instances
  • Total string length between 1 and 120 characters
  • Other allowed characters are alphanumeric only

Then a regex of [@A-Za-z0-9]{1,120} should do the job for you.