When validating an email address with the regex valida开发者_如何学Ction component, an additional RequiredFieldValidator must be added to ensure there is a value present.
I've mostly taken care of this with a CustomFieldValidator, and taking care of this with Javascript.
Is there a better way of doing this?
Why wouldn't you just use the RegularExpressionValidator
and the RequiredFieldValidator
?
If you use a CustomFieldValidator
you will need to implement a javascript check and a server side check as well. Using the other two validation controls together need no extra implementation except for a couple of attributes being set and it is the expected way of doing this type of validation with WebForms.
Consider the next programmer that is going to come along and see your different setup and wonder why you went to all this extra work when none of it was needed.
If you fancy doing it in the background code you could use the following function:
Function checkEMail(ByVal email As String) As Boolean
Dim pattern As String = "^((?>[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+\x20*|""((?=[\x01-\x7f])[^""\\]|\\[\x01-\x7f])*""\x20*)*(?<angle><))?((?!\.)(?>\.?[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+)+|""((?=[\x01-\x7f])[^""\\]|\\[\x01-\x7f])*"")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\[\x01-\x7f])+)\])(?(angle)>)$"
Dim emailCheck As Match = Regex.Match(email, pattern)
If emailCheck.Success Then
checkEMail = True
Else
checkEMail = False
End If
Return checkEMail
End Function
精彩评论