检查输入的错误字符、NaN、长度

Check inputs for bad characters, NaN, length

我正在搜索一般输入检查,我已经找到了一些类似 NaN 的东西,或者如果它太短但是你怎么能检查是否有像 ($§?=)[= 这样的字符23=] 您不希望输入的内容是?

我的第二个问题是,如果您想使用输入重命名文件,检查什么很重要。除了长度。

编辑:

if($ParID -lt 6) {
        $specific_error = "Par ID is is too short!"
    } else { #else 1
        if(!($ParID -match "[1-999999]")) {
            $specific_error = "Par ID must only contain numbers!"
        } else { #else 2

        }#else 2
    } #else 1

编辑 2:

$ParIDInbox = New-Object System.Windows.Forms.TextBox #initialization -> initializes the input box

$ParIDInbox.Location = New-Object System.Drawing.Size(10,30) #Location -> where the label is located in the window

$ParIDInbox.Size = New-Object System.Drawing.Size(260,20) #Size -> defines the size of the inputbox

$ParIDInbox.MaxLength = 6 #sets max. length of the input box to 6 

要检查文件名是否包含任何不需要的字符,您可以使用 regex:

$invalidCharacter = '$§?='
[regex]$invalidCharacter = '[{0}]' -f ([regex]::Escape($invalidCharacter))
if ($invalidCharacter.IsMatch($yourFileName))
{
    # filename contains some invalid character ...
}

为确保文件名有效,您可以使用 GetInvalidFileNameChars .NET 方法检索所有无效字符并再次使用 regex 检查文件名是否有效:

[regex]$containsInvalidCharacter = '[{0}]' -f ([regex]::Escape([System.IO.Path]::GetInvalidFileNameChars()))
$yourFileName = 'invali?idFilename.txt'

if ($containsInvalidCharacter.IsMatch($yourFileName))
{
    # filename is invalid...
}