1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / ValidatorDefinitionElement - Regular expression validation runs for empty field

ValidatorDefinitionElement - Regular expression validation runs for empty field

4 posts, 0 answered
  1. Antoine
    Antoine avatar
    45 posts
    Registered:
    13 Jul 2012
    25 Jul 2011
    Link to this post
    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*[-\/\.]?)?(\((\d{3})\)|(\d{3}))\s*[-\/\.]?\s*(\d{3})\s*[-\/\.]?\s*(\d{4})\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
  2. Antoine
    Antoine avatar
    45 posts
    Registered:
    13 Jul 2012
    25 Jul 2011
    Link to this post
    P.S.  I am working with Sitefinity 4.1 SP2
  3. Stanislav Velikov
    Stanislav Velikov avatar
    1113 posts
    Registered:
    18 Aug 2017
    27 Jul 2011
    Link to this post
    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
  4. Antoine
    Antoine avatar
    45 posts
    Registered:
    13 Jul 2012
    28 Jul 2011
    Link to this post
    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 = @"(^$)|(^\(\d{3}\) \d{3}-\d{4}$)",
        RegularExpressionViolationMessage = Res.Get<ContactsResources>().InvalidPhone
    };

4 posts, 0 answered