Validation.RegExPatterns.Email and String.Empty

Validation.RegExPatterns.Email and String.Empty

Old forum URL: forums.lhotka.net/forums/t/1822.aspx


Norty posted on Thursday, November 23, 2006

One of the fields that can captured in my app is an email address.  This field is not compulsory.  However, if it is captured, then it must be a valid email address with a maximum length of 100 characters.  My Validation works great when an email address is captured, but throws an exception if it is left blank.

Is there a quick fix for this problem? Confused [8-)] 

If not, I would like to add Validation.RegExPatterns.EmailOptional to the CSLA library with a modified RegEx to allow for empty strings.  Any help on modifying the existing pattern ("^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$") would be appreciated. Smile [:)]

 

Protected Overrides Sub AddBusinessRules()

ValidationRules.AddRule(AddressOf Validation.CommonRules.StringMaxLength, New Validation.CommonRules.MaxLengthRuleArgs("Email", 100))

ValidationRules.AddRule(AddressOf Validation.CommonRules.RegExMatch, New Validation.CommonRules.RegExRuleArgs("Email", Validation.RegExPatterns.Email))

End Sub

Des

RRorije replied on Thursday, November 23, 2006

I have had the same problem, and I think the solution to any regex pattern to be optional is to use the non-optional regex pattern and add: |^$. In language I think it means "OR begin of string followed by end of string". But I am not a Regex guru by any means. But using some tests seemed this is correct.

So your Email pattern will then be:
"^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$ | ^$"


Ruben

Bayu replied on Thursday, November 23, 2006

RRorije:
I have had the same problem, and I think the solution to any regex pattern to be optional is to use the non-optional regex pattern and add: |^$. In language I think it means "OR begin of string followed by end of string". But I am not a Regex guru by any means. But using some tests seemed this is correct.

So your Email pattern will then be:
"^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$ | ^$"


Ruben


Hey,

We posted almost simultaneously. ;-)

As a comment on your post: I don't think it's wise to 'loosen up' your regex. Rather surround the regex match test in a if-then-else condition that checks for empty strings before applying the regex.

See also my previous post.

Bayu

RRorije replied on Thursday, November 23, 2006

I totally agree with your last but one post. I think it is better to have a rule that uses the regex to check whether some value is correct.

And on top of that, if necessary, a rule when the property is required. That way you don't end up with several regex-patterns which come down to required/not required. This is cleaner I think

Norty replied on Thursday, November 23, 2006

Thanks Guys

Just thought it would be easier/quicker to use common rules.Sad [:(]  I'll just implement a custom validation procedure.

Des

ajj3085 replied on Monday, November 27, 2006

Well as I said in my other post, you can.  If the string is not empty you call the standard regex validator from CommonRules and return its result.

Bayu replied on Thursday, November 23, 2006

Instead of using the standard RegexMatch validation in CommonRules you could create your own validation function. Within this function you would then be free to apply any logic that you wish, hence also to check if a string is not empty before matching the regex.

In the ProjectTracker sample there is a Project BO which makes use of such a function.

Bayu

ajj3085 replied on Monday, November 27, 2006

I'd recommend Bayu's approach, and in fact is the approach I have taken with the same requirement.

In my custom logic, I check if the string is empty and if it is, return that the rule is met.  If its not empty, I end up calling the RegEx validator and returning that result.

HTH
Andy

Copyright (c) Marimer LLC