validationenterprise-library-5

Validation with validation block


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


Solution

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

    If your requirements are:

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