Skip to main content
InSource Solutions

TN AG115 Validate User Input with Regular Expressions (RegEx)

insource logo large.jpg

 

Description

Regular Expressions are a way to compare a value to see if it is in a particular format.  This can be useful to validate user input to ensure it is formatted correctly.

 

  • Author: Dillon Perera
  • Published: 12/31/2015
  • Applies to: ArchestrA Symbols with InTouch

 

 

Details

To use Regular Expressions we will make use of the .NET System.Text.RegularExpressions library.

 

The following lines of script are the basic format we will use to validate input, replacing the FORMATSTRING with the required format, and MyString with the variable we are trying to validate.

'This variable will contain the definition of the format we are looking for
dim regex as System.Text.RegularExpressions.Regex;
'This variable will be used to test if the desired format is found
dim match as System.Text.RegularExpressions.Match;


'Define the actual format we are looking for
regex = new System.Text.RegularExpressions.Regex("FORMATSTRING");
'Compare actual input against the desired format, see if they match
match = regex.Match(MyString);

 

RegEx_GeneralSyntax2.png

 

Details on how to format a Regular Expression's string can be found online.  The following MSDN page gives a number of examples: https://msdn.microsoft.com/en-us/library/ae5bf541(v=vs.90).aspx

 

 

Example:

 

User is requested to enter a Batch ID.  We know that for this company all Batch IDs must begin with a capital B, followed by three digits, a single uppercase character indicating the product code, then 1-3 lowercase letters indicating the country it is going to.

 

We will now build the regular expression:

Rule Equivalent in Regular Expression Syntax
Must start with capital B [B] - indicating we're looking for 1 character that must be 'B'
Three digits \d{3} - \d indicates a number, {3} is the quantity
A single uppercase letter [A-Z] - indicates we'll accept a character between A and Z.
1 to 3 lowercase letters [a-z]{1,3} - indicates we'll accept a character between 'a' and 'z', and the {1,3} indicates we allow 1 to 3 characters.

 So the final regular expression will be "[B]\d{3}[A-Z][a-z]{1,3}".

 

In our ArchestrA symbol we might create an Action Script similar to the following (Highlighted lines show differences from original script) to only process the next batch if we have received a valid BatchID.

 

'This variable will contain the definition of the format we are looking for
dim regex as System.Text.RegularExpressions.Regex;
'This variable will be used to test if the desired format is found
dim match as System.Text.RegularExpressions.Match;

'Define the actual format we are looking for
regex = new System.Text.RegularExpressions.Regex("[B]\d{3}[A-Z][a-z]{1,3}");

'Compare actual input against the desired format, see if they match
match = regex.Match(MyString);

'Create a boolean to check if we have a match
dim result as boolean;
result = match.Success;

'If it is a valid BatchID begin a new batch
if (result == true) then
    MyArea.BatchID = MyString;
    MyArea.StartNewBatch = 1;
else
    {Do nothing}
endif;

 

RegEx_ExampleCode.png

 

With this code, the script will compare the BatchID entered by the user against the existing format.  Below are some examples of potential input, and if they would be accepted:

Input from User Accepted? If not, why?
B712Ausa Yes  
A615Ausa
 
No Begins with an 'A', not a 'B'
b123Bcdn
 
No Begins with a lowercase 'b', not uppercase 'B'
B123Bcdn
 
Yes  
BabcCusa
 
No After the 'B' there are letters instead of numbers
B51Dusa
 
No After the 'B' there are only two numbers, not three
B517Dusa
 
Yes  
B517DUSA
 
No The last three letters are uppercase, not lowercase