Skip to content

Using Class Extensions to Validate Email and URL Data

Often in our projects we need to apply some kind of validation of the user input so we get the data we are expecting. For example, getting a valid email addresses or URL. I want to share with you a couple of String Class extensions you can use to see (and conform) whether the user typed a valid email or URL, this is not a silver bullet (there are always exceptions), but it can save you some time now and in future projects.

Before we start, let’s remember that a Class extension is a really convenient way to provide additional functionality to an existing class without needing to subclass it.

So, the first thing is adding a new Module to your project (you may name it “String Extensions”, for example), and a couple of new Global methods on it.

Let’s name the first of these methods as “ValidatedEmail”, using the following signature in the Inspector Panel:

  • Parameters: Value as String
  • Return Type: String

Then, put the following snippet of code in the associated Code Editor for the Method:

// Some previous text cleaning

Value = Value.Trim
Value = Value.ReplaceAll("""","")
Value = Value.ReplaceAll(" ","")
Value = Value.ReplaceAll(Chr(9),"")
Value = Value.ReplaceAll(Chr(10),"")
Value = Value.ReplaceAll(Chr(13),"")

If Value.Left(1) < Chr(48) Then 
  Value = Value.Right(Value.Length-1)
End If

If Value.EndsWith(".") Then
  Value = Value.Left(Value.Length-1)
End If

// Look for a proper email format
Var rx As New RegEx
rx.SearchPattern = "^([a-zA-Z0-9_\-.\+]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,10})$"

Var match As RegExMatch = rx.Search(source)

Return If(match <> Nil, Value, "")

As you can see from the code, it uses the power of Regular Expressions to search the expected format for an email address; if it returns a match, then that means that we are dealing with a valid email address.

Add now the second Method and name it “ValidatedURL”, using the following signature in the Inspector Panel:

  • Parameters: Value as String
  • Return Type: Boolean

And put the following piece of code in the associated Code Editor:

Value = Value.Trim

If (Not value.BeginsWith("http://")) and (Not value.BeginsWith("https://")) Then Value = "http://"+Value

Var rx As New RegEx
rx.SearchPattern = "^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$"

Var match As RegExMatch = rx.Search(Value)

Return if(match <> Nil, Value,"")

As you can see, we are using the same technique here, changing the pattern string we are using for the search of a valid URL address in the data provided as “source”.

Once done, you can start using these new method extensions to validate the input data from the user. As previously said, these are not silver bullets … but there is a good chance that most of the wrong cases are caught and returned as not valid.

For example, you can use them like this:

Var email, webaddress As String

email = EmailField.value // EmailField is a TextField
webaddress = WebAddressField.value // WebAddress is a TextField

email = ValidatedEmail(email)


webaddress = validatedURL(webaddress)

// The string has the expected URL format.

End If

What it does?
Well, in the case that the provided data can be conformed to a valid email or URL, then it will return the string, with the original data modified in the case there was needed to do some “cleaning” (for example “email@domain.com. ” has a valid email inside always we remove the extra space and final dot here). Why to do this? Well, sometimes (in my own experience), users tend to type a final dot, an extra space or something like that; so… without that kind of “cleaning” in the original data, a probably good email or URL would be tested as an invalid one).

Obviously you can put two extra methods just for those cases when you want to validate if the provided data is an email or URL, without changes and messing with the original data. In that case these methods will return just True or False.

Let’s name the first of these methods as “IsEmail”, using the following signature in the Inspector Panel:

  • Parameters: Value as String
  • Return Type: Boolean

Then, put the following snippet of code in the associated Code Editor for the Method:

// Look for a proper email format
Var rx As New RegEx
rx.SearchPattern = "^([a-zA-Z0-9_\-.\+]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,10})$"

Var match As RegExMatch = rx.Search(Value)

Return If(match <> Nil, True, False)

Add now the second Method and name it “IsURL”, using the following signature in the Inspector Panel:

  • Parameters: Value as String
  • Return Type: Boolean

And put the following piece of code in the associated Code Editor:

Var rx As New RegEx
rx.SearchPattern = "^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$"

Var match As RegExMatch = rx.Search(Value)

Return if(match <> Nil, True,False)

I hope you find this useful and, of course, you can continue adding more useful Method Extensions to String or other existing classes in your Modules so you can work with these anytime you need them in your projects … without writing the code again; and without resorting into subclassing.