ValidatorDefinitionElement - Regular expression validation runs for empty field
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
;
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
;
P.S. I am working with Sitefinity 4.1 SP2
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
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
;