I simply want to validate that a parameter is only made up of alphanumeric characters. I've tried this, but it doesn't behave as I'd like:
param(
[ValidatePattern('[a-zA-Z0-9]')]$someVariableThatShouldOnlyContainAlphaNumerics = 'something_with_underscores'
)
write-host $someVariableThatShouldOnlyContainAlphaNumerics
Returns:
something_with_underscores
I'm clearly missing something obvious. Any suggestions?
ValidatePattern
supports regex. If the pattern matches the string is validated. You are not validating all characters in the string... just one !
Changing the regex pattern to match the entire string would be one approach. Other regex will also suffice.
[ValidatePattern('^[a-zA-Z0-9]+$')]$someVariableThatShouldOnlyContainAlphaNumerics = 'something_with_underscores'
Note that setting a default will bypass this validation as it is not being passed to the function.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.