0

When validating an email address with the regex validation 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?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.