Victor Arevalo
Victor Arevalo

Reputation: 45

Verifying a string is not empty/null in an If condition

I'm writing a script that will accept user input via Read-Host (set to $String), and I want to avoid any issues that could be caused by having a blank value for the variables. Since I'll be using this a lot, I want to implement it into a function that verifies no invalid characters are being used.

I thought I could use an if statement with ![string]::IsNullOrEmpty($String) as one of the conditions:

Function Test-ValidCharacters ($String, $ValidCharacters) {  
  if (($String -match $ValidCharacters) -and (!([string]::IsNullOrEmpty($String)))) {
    return $true
  }
  else {return $false}
}

I also tried this:

Function Test-ValidCharacters ($String, $ValidCharacters) {  
  if (($String -match $ValidCharacters) -and ($String -ceq "")) {
    return $true
  }
  else {return $false}
}

In both of these cases, I can just hit enter when presented with the $String's Read-Host prompt and the script will behave as if the function returned $True (and then later encounter fatal errors). The other half works - if I include characters not specified by $ValidCharacters the function returns $False as expected.

I am sure I'm missing something here. I even tried doing a second nested if statement and got the same result.

Edit: Here's the code snippet where I call the function and notice the issue.

$ValidCharacters = '[^a-zA-Z0-9]'
$FirstN = Read-Host -Prompt "New user's first name"
While (Test-ValidCharacters $FirstN $ValidCharacters -eq $false) {
  Write-Output "$FirstN contains illegal characters. A-Z, a-z, and 0-9 are accepted."
  $FirstN = Read-Host -Prompt "New user's first name"
}

Upvotes: 2

Views: 5308

Answers (1)

mklement0
mklement0

Reputation: 437803

Assuming $ValidCharacters isn't itself an empty string and contains an anchored character-range regex (regular expression) that covers the entire input string, such as ^[a-z0-9./:]+$, given that the -match operator matches any substring by default (note that a better name for the parameter is therefore something like $ValidationRegex):[1]

  • In the first function definition, the RHS of your -and operation is redundant - it adds nothing to the conditional, because if $String -match $ValidCharacters is $true, then so is ! [string]::IsNullOrEmpty($String), by definition.

  • Conversely, in the second function definition your -and operation always returns $false, because $String -ceq "" is by definition $false, if the LHS returned $true.

Assuming that your intent is to prevent empty or all-whitespace input and to ensure that any string - trimmed of incidental leading and/or trailing whitespace - is composed only of expected characters, use the following:

Function Test-ValidCharacters ($String, $ValidCharacters) {
  # Note: no strict need for `return`.
  $String.Trim() -match $ValidCharacters
}

[1] Alternatively, stick with $ValidCharacters and pass a regex that describes only a single valid character, such as '[a-z0-9./:]', and construct the entire-string matching regex inside the function with '^' + $ValidCharacters + '+$'

Upvotes: 3

Related Questions