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.I can give you some short examples here how to create and read all content from a MySQL database. Rx.SearchPattern = match As RegExMatch = rx.Search(Value)Īdd now the second Method and name it “IsURL”, using the following signature in the Inspector Panel:Īnd put the following piece of code in the associated Code Editor: Var rx As New RegEx Then, put the following snippet of code in the associated Code Editor for the Method: // Look for a proper email format Let’s name the first of these methods as “IsEmail”, using the following signature in the Inspector Panel: In that case these methods will return just True or False. 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. 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). 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 ” has a valid email inside always we remove the extra space and final dot here). The string has the expected URL format. Webaddress = WebAddressField.value // WebAddress is a TextField 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.įor example, you can use them like this: Var email, webaddress As StringĮmail = EmailField.value // EmailField is a TextField Once done, you can start using these new method extensions to validate the input data from the user. Rx.SearchPattern = "^(http:\/\/Var match As RegExMatch = rx.Search(Value)Īs 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”. If (Not value.BeginsWith(" and (Not value.BeginsWith(" Then Value = " Var rx As New RegEx Then, put the following snippet of code in the associated Code Editor for the Method: // Some previous text cleaningĪs 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.Īdd now the second Method and name it “ValidatedURL”, using the following signature in the Inspector Panel:Īnd put the following piece of code in the associated Code Editor: Value = Value.Trim Let’s name the first of these methods as “ValidatedEmail”, using the following signature in the Inspector Panel: 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. 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.īefore 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. For example, getting a valid email addresses or URL. Often in our projects we need to apply some kind of validation of the user input so we get the data we are expecting.