AutoStore: AutoCapture Validate Regex
It is possible to check the field input of an AutoCapture form as it is submitted. In the following example, we begin with an AutoCapture form String (text) field named "Email To".
Make sure the Run on form validate option is enabled. Doing so will execute the Form_OnValidate event handler. Here, we can pass the field value with a regular expression pattern into a custom function named IsValidRegExp(), which returns a True or False.
Const REGEX_PATTERN = "^\S+@\S+$"
Const FIELD_TO_TEST = "Email To"
Function Form_OnValidate(Form)
' Get the field value ...
Dim strFieldValue : strFieldValue = Form.GetFieldValue(FIELD_TO_TEST)
' Test the field value ...
If Not IsValidRegExp(strFieldValue, REGEX_PATTERN) Then
Form_OnValidate = "Please check the [" & FIELD_TO_TEST & "] field entry."
End If
End Function
objRegExp.Global = True
If Not objRegExp.Test(TestValue) Then
IsValidRegExp = False
Else
IsValidRegExp = True
End If
If the input value does not contain an ampersand, the form will not submit.
Let's take a closer look at the regular expression pattern used for testing. It will match if the input contains an ampersand (@) along with one or more characters before and after it.
For matching an email address, this pattern is not overly restrictive. It's worth mentioning that building an regular expression pattern to test for a valid email address is not only a challenge, but is a highly debated topic.
Consider the growing list of top level domains now available. Gone are the days where .com, .net, .org, .edu, .gov, and .mil where almost exclusively in use. Now, countries have their own top-level domain, plus over a thousand new custom domain names.
The goal for this post is to describe a simple approach to input validation in an AutoCapture form. In fact, with a few modifications to the constants in the script, validating common entries like a phone number, a zip code, or even other custom input types, is also possible.
Make sure the Run on form validate option is enabled. Doing so will execute the Form_OnValidate event handler. Here, we can pass the field value with a regular expression pattern into a custom function named IsValidRegExp(), which returns a True or False.
AutoCapture Form Validate RegEx Example
'================================================================================
' CONSTANTS SECTION
'--------------------------------------------------------------------------------
Const REGEX_PATTERN = "^\S+@\S+$"
Const FIELD_TO_TEST = "Email To"
'================================================================================
' EVENT PROCEDURES
'--------------------------------------------------------------------------------
Function Form_OnValidate(Form)
' Get the field value ...
Dim strFieldValue : strFieldValue = Form.GetFieldValue(FIELD_TO_TEST)
' Test the field value ...
If Not IsValidRegExp(strFieldValue, REGEX_PATTERN) Then
Form_OnValidate = "Please check the [" & FIELD_TO_TEST & "] field entry."
End If
End Function
'================================================================================
' HELPER FUNCTIONS
'--------------------------------------------------------------------------------
' Test a Regular Expression pattern, Return True or False
Function IsValidRegExp(TestValue, TestExpression)
Set objRegExp = CreateObject("VBScript.RegExp")
objRegExp.Pattern = TestExpressionobjRegExp.Global = True
If Not objRegExp.Test(TestValue) Then
IsValidRegExp = False
Else
IsValidRegExp = True
End If
End Function
If the input value does not contain an ampersand, the form will not submit.
Let's take a closer look at the regular expression pattern used for testing. It will match if the input contains an ampersand (@) along with one or more characters before and after it.
Const REGEX_PATTERN = "^\S+@\S+$"
'--------------------------------------------------------------------------------
' ^ <-- Match the beginning of the string
' \S <-- Match any character that is not a whitespace character
' + <-- Match 1 or more in the preceding Character Set
' @ <-- Match a literal ampersand character
' \S <-- Match any character that is not a whitespace character
' + <-- Match 1 or more in the preceding Character Set
' $ <-- Match the end of the string
For matching an email address, this pattern is not overly restrictive. It's worth mentioning that building an regular expression pattern to test for a valid email address is not only a challenge, but is a highly debated topic.
Consider the growing list of top level domains now available. Gone are the days where .com, .net, .org, .edu, .gov, and .mil where almost exclusively in use. Now, countries have their own top-level domain, plus over a thousand new custom domain names.
The goal for this post is to describe a simple approach to input validation in an AutoCapture form. In fact, with a few modifications to the constants in the script, validating common entries like a phone number, a zip code, or even other custom input types, is also possible.
Comments
Post a Comment