Input validation on multiple regex

One of the things I like about writing advanced functions is the ability to validate the input. if you test the input immediately you can  often stop mistakes being made. Mistakes that could damage your system!

One of the validation options is to test the input against a regular expression. Not being a big fan of regular expressions I don’t use this often but one option that came up on the forum was testing against more than one regex.

DISCLAIMER – The regex I’m using aer for illustrative purposes only and I don’t claim they are the best way of doing this.

This function uses a simple regex to validate the input starts with a letter:

function Test-Pval
{
  [CmdletBinding()]
  Param
  (
    [ValidatePattern(“^[a-z]”)]
    [String]
    $parm1
  )
  Write-Host “Input was $parm1”
}

£> Test-Pval abcd
Input was abcd

£> Test-Pval Abcd
Input was Abcd

£> Test-Pval 2bcd
Test-Pval : Cannot validate argument on parameter ‘parm1’. The argument “2bcd” does not match the “^[a-z]” pattern.
Supply an argument that matches “^[a-z]” and try the command again.
At line:1 char:11
+ Test-Pval 2bcd
+           ~~~~
    + CategoryInfo          : InvalidData: (:) [Test-Pval], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Test-Pval

 

Lets also assume that we want to be able to accept input starting with @ or #

function Test-Pval
{
  [CmdletBinding()]
  Param
  (
    #[ValidatePattern(“^[a-z]”)]
    [ValidatePattern(“^[@,#]”)]
    [String]
    $parm1
  )
  Write-Host “Input was $parm1”
}

£> Test-Pval 2bcd
Test-Pval : Cannot validate argument on parameter ‘parm1’. The argument “2bcd” does not match the “^[@,#]” pattern.
Supply an argument that matches “^[@,#]” and try the command again.
At line:1 char:11
+ Test-Pval 2bcd
+           ~~~~
    + CategoryInfo          : InvalidData: (:) [Test-Pval], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Test-Pval
 

£> Test-Pval abcd
Test-Pval : Cannot validate argument on parameter ‘parm1’. The argument “abcd” does not match the “^[@,#]” pattern.
Supply an argument that matches “^[@,#]” and try the command again.
At line:1 char:11
+ Test-Pval abcd
+           ~~~~
    + CategoryInfo          : InvalidData: (:) [Test-Pval], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Test-Pval
 

£> Test-Pval @bcd
Test-Pval : Cannot validate argument on parameter ‘parm1’. The argument “” does not match the “^[@,#]” pattern.
Supply an argument that matches “^[@,#]” and try the command again.
At line:1 char:11
+ Test-Pval @bcd
+           ~~~~
    + CategoryInfo          : InvalidData: (:) [Test-Pval], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Test-Pval
 

£> Test-Pval ‘@bcd’
Input was @bcd

£> Test-Pval ‘#bcd’
Input was #bcd

£> Test-Pval ‘?bcd’
Test-Pval : Cannot validate argument on parameter ‘parm1’. The argument “?bcd” does not match the “^[@,#]” pattern.
Supply an argument that matches “^[@,#]” and try the command again.
At line:1 char:11
+ Test-Pval ‘?bcd’
+           ~~~~~~
    + CategoryInfo          : InvalidData: (:) [Test-Pval], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Test-Pval

 

The previous acceptable input is now rejected and only input starting with @ or # is accepted.

To combine the 2 patterns we use the | symbol which is an OR operator in regex

function Test-Pval
{
  [CmdletBinding()]
  Param
  (
    [ValidatePattern(“^[a-z | @,#]”)]
    [String]
    $parm1
  )
  Write-Host “Input was $parm1”
}

function Test-Pval
{
  [CmdletBinding()]
  Param
  (
    [ValidatePattern(“^[a-z | @,#]”)]
    [String]
    $parm1
  )
  Write-Host “Input was $parm1”
}

Which when used give this

£> Test-Pval abcd
Input was abcd

£> Test-Pval Abcd
Input was Abcd

£> Test-Pval 2bcd
Test-Pval : Cannot validate argument on parameter ‘parm1’. The argument “2bcd” does not match the “^[a-z | @,#]”
pattern. Supply an argument that matches “^[a-z | @,#]” and try the command again.
At line:1 char:11
+ Test-Pval 2bcd
+           ~~~~
    + CategoryInfo          : InvalidData: (:) [Test-Pval], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Test-Pval
 

£> Test-Pval ‘@bcd’
Input was @bcd

£> Test-Pval ‘#bcd’
Input was #bcd

£> Test-Pval ‘?bcd’
Test-Pval : Cannot validate argument on parameter ‘parm1’. The argument “?bcd” does not match the “^[a-z | @,#]”
pattern. Supply an argument that matches “^[a-z | @,#]” and try the command again.
At line:1 char:11
+ Test-Pval ‘?bcd’
+           ~~~~~~
    + CategoryInfo          : InvalidData: (:) [Test-Pval], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Test-Pval

Expect to spend a long time figuring out the regex though Smile

This entry was posted in Powershell Basics. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s