ValidatorDefinitionElement - Regular expression validation r

Posted by Community Admin on 04-Aug-2018 19:22

ValidatorDefinitionElement - Regular expression validation runs for empty field

All Replies

Posted by Community Admin on 25-Jul-2011 00:00

Hi

I'm building a custom module and am using the ValidatorDefinitionElement to enforce validation on my content items' fields.  I have fields that are not required, but when information is entered, should be validated.  I am doing this in two ways:

1.  For my email field, I am using the ExpectedFormat property to specify that the field should contain email values.

emailField.ValidatorConfig = new ValidatorDefinitionElement(emailField)
    Required = false,
    MessageCssClass = "sfError",
    ExpectedFormat = ValidationFormat.EmailAddress,
    EmailAddressViolationMessage = Res.Get<ContactsResources>().InvalidEmailAddress
;

2.  For my phone field, I am using the RegularExpression property to specify that the field should contain phone numbers.
phoneField.ValidatorConfig = new ValidatorDefinitionElement(phoneField)
    Required = false,
    MessageCssClass = "sfError",
    RegularExpression = @"^(1\s*[-\/\.]?)?(\((\d3)\)|(\d3))\s*[-\/\.]?\s*(\d3)\s*[-\/\.]?\s*(\d4)\s*(([xX]|[eE][xX][tT])\.?\s*(\d+))*$",
    RegularExpressionViolationMessage = Res.Get<ContactsResources>().InvalidPhone
;

In both cases, the expression validation is run even if there is no value in the field.  I would like it to run only when there is a value.  Setting the Required property to false does not accomplish this.

Is this a bug or expected behavior?  What is the best way to accomplish what I am trying to do?

Thanks

Posted by Community Admin on 25-Jul-2011 00:00

P.S.  I am working with Sitefinity 4.1 SP2

Posted by Community Admin on 27-Jul-2011 00:00

Hello Antoine,

To validate a field it should be required=true. In your case I recommend using a condition. If the field is != null validate and if not don`t validate.

Greetings,
Stanislav Velikov
the Telerik team

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 28-Jul-2011 00:00

I resolved the issue by adding an option for null values (^$) in my regular expression.

phoneField.ValidatorConfig = new ValidatorDefinitionElement(phoneField)
    Required = false,
    MessageCssClass = "sfError",
    RegularExpression = @"(^$)|(^\(\d3\) \d3-\d4$)",
    RegularExpressionViolationMessage = Res.Get<ContactsResources>().InvalidPhone
;

This thread is closed