Skip to main content

Regular Expression Example in a VB.NET Validation Script


QAID # 18261 Published

Question / Problem:

Is there an example of using a Regular Expression in a VB.NET Validation Script?

Answer / Solution:

Below is an example using the RegEx class in the System.Text.RegularExpressions library to restrict the input of an Index Field to 9 or 10 numeric characters:

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Kofax.AscentCapture.Scripting
Imports Kofax.AscentCaptureModule
Imports System.Text.RegularExpressions
Namespace Test
    <SuppressFieldEventsOnDocClose(false)> _
        Public Class Test
            Inherits DocumentValidationScript
            <IndexFieldVariableAttribute("SS Number")> _
            Dim WithEvents SS_Number As FieldScript
            <IndexFieldVariableAttribute("Name")> _
            Dim WithEvents Name As FieldScript
            <IndexFieldVariableAttribute("Address")> _
            Dim WithEvents Address As FieldScript
            <IndexFieldVariableAttribute("State")> _
            Dim WithEvents State As FieldScript
            Private Sub SS_Number_FieldPostProcessing(sender As Object,
                                e As Kofax.AscentCapture.Scripting.PostFieldEventArgs)
                                Handles SS_Number.FieldPostProcessing
            If Not Regex.IsMatch(SS_Number.IndexField.Value, "(^[0-9]{9,10}$)") Then
                Throw New ValidationErrorException("Invalid SS Number")
            End If
        End Sub
    End Class
End Namespace

Note in the Includes section where we have included System.Text.RegularExpressions. RegEx is a static class which allows its methods to be called directly.

Applies to:

Product Version