Regular Expressions / Regex in Property Validation VBScript

Hi, I've just got something working and didn't find similar examples of this on forum so wanted to share

We have a text property that users can enter a Code in. It's text but needs to be alphanumeric only, so A-Z, a-z, 0-9. 
We want to disallow special characters like brackets, punctuation, etc

We also want to limit it to a character length limit

This goes on the Property definition, in the Validation tab, 'Validation with VBScript':

' Check code length
If Len(PropertyValue.GetValueAsUnlocalizedText) <> 2 Then
    Err.Raise MFScriptCancel, "Please enter a Code that is exactly 2 characters long."
End If


' Check it's all alphanumeric
Dim regexCode : Set regexCode = New RegExp
regexCode.Pattern = "[^a-zA-Z0-9]"

If regexCode.Test(PropertyValue.GetValueAsUnlocalizedText) Then
    Err.Raise MFScriptCancel, "Please enter a Code using only letters and numbers"
End If

Parents Reply Children