Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, September 16, 2015 7:34 PM
Hey Guys,
Working on a user input piece, and I need to validate that the first character in the string is a letter. I've been dinking around with -
$inputbox = [Microsoft.VisualBasic.Interaction]::InputBox("Please enter your Site location:", "Test Script")
if(!($inputbox.StartsWith("[a-z]^[A-Z]")){
do{
$inputbox = [Microsoft.VisualBasic.Interaction]::InputBox("Your site location should start with a letter and be $len characters long. Please enter your Site location:", "Test Script")
}while(!($inputbox.StartsWith("[a-z]^[A-Z]"))
}
I feel like what I have above will only check if it starts with a. I could do something like this as well but not sure -
($inputbox[0] -notmatch "[a-z]^[A-Z]")
I could create an alphabet array and loop through that against [0] as well? Any other elegant ways to handle this? Thanks :)
Ryan
All replies (5)
Wednesday, September 16, 2015 7:48 PM ✅Answered | 4 votes
No need fo r striners just match
$inputbox -match '^[a-zA-Z]'
You can also use the masked text control to do this with a pattern.
You also cannot put the comma in the range or it will also be matched. Only the dash is allowd hear. A list is just a list [abcdxyz]
\(ツ)_/
Wednesday, September 16, 2015 7:43 PM
Hello,
Regex in powershell is case insensitive by default so you can match it like this:
$test = 'Testvalue'
$test[0] -match "[a-z]"
SingleHop: A leader in Managed Azure
Wednesday, September 16, 2015 7:46 PM
Hi Ryan,
try like the following:
"0T1".Substring(0,1) -match "[a-z,A-Z]"
$inputbox.Substring(0,1) -match "[a-z,A-Z]"
Greetings,
David das Neves
Technology Specialist - Consulting Services
Computacenter AG & Co. oHG - München
Caution: This post may contain errors.
Wednesday, September 16, 2015 8:11 PM
Here is a very basic example of a masked textbox input dialog
function Call-Test-MaskedEdit{
[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[System.Windows.Forms.Application]::EnableVisualStyles()
$form1 = New-Object 'System.Windows.Forms.Form'
$labelEnterUpTo8Aplhanumer = New-Object 'System.Windows.Forms.Label'
$buttonCancel = New-Object 'System.Windows.Forms.Button'
$maskedtextbox1 = New-Object 'System.Windows.Forms.MaskedTextBox'
$buttonOK = New-Object 'System.Windows.Forms.Button'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
$Form_StateCorrection_Load ={$form1.WindowState = $InitialFormWindowState}
$form1.SuspendLayout()
$form1.Controls.Add($labelEnterUpTo8Aplhanumer)
$form1.Controls.Add($buttonCancel)
$form1.Controls.Add($maskedtextbox1)
$form1.Controls.Add($buttonOK)
$form1.AcceptButton = $buttonOK
$form1.ClientSize = '284, 127'
$form1.FormBorderStyle = 'FixedDialog'
$form1.MaximizeBox = $False
$form1.MinimizeBox = $False
$form1.Name = 'form1'
$form1.StartPosition = 'CenterScreen'
$form1.Text = 'Form'
$labelEnterUpTo8Aplhanumer.Font = 'Microsoft Sans Serif, 9.75pt, style=Bold, Italic'
$labelEnterUpTo8Aplhanumer.Location = '21, 9'
$labelEnterUpTo8Aplhanumer.Name = 'labelEnterUpTo8Aplhanumer'
$labelEnterUpTo8Aplhanumer.Size = '240, 38'
$labelEnterUpTo8Aplhanumer.TabIndex = -1
$labelEnterUpTo8Aplhanumer.Text = 'Enter up to 8 aplha-numeris and must begin with a letter.
'
$labelEnterUpTo8Aplhanumer.TextAlign = 'MiddleCenter'
# buttonCancel
$buttonCancel.DialogResult = 'Cancel'
$buttonCancel.Location = '173, 92'
$buttonCancel.Name = 'buttonCancel'
$buttonCancel.Size = '75, 23'
$buttonCancel.TabIndex = -1
$buttonCancel.Text = 'Cancel'
$buttonCancel.UseVisualStyleBackColor = $True
# maskedtextbox1
$maskedtextbox1.Location = '99, 50'
$maskedtextbox1.Mask = 'Laaaaaaa'
$maskedtextbox1.Name = 'maskedtextbox1'
$maskedtextbox1.Size = '74, 20'
$maskedtextbox1.TabIndex = 1
# buttonOK
$buttonOK.Anchor = 'Bottom, Right'
$buttonOK.DialogResult = 'OK'
$buttonOK.Location = '32, 92'
$buttonOK.Name = 'buttonOK'
$buttonOK.Size = '75, 23'
$buttonOK.TabIndex = -1
$buttonOK.Text = '&OK'
$buttonOK.UseVisualStyleBackColor = $True
$form1.ResumeLayout()
$InitialFormWindowState = $form1.WindowState
$form1.add_Load($Form_StateCorrection_Load)
return $form1.ShowDialog()
}
Call-Test-MaskedEdit
\(ツ)_/
Thursday, September 17, 2015 3:22 PM
Thanks for your responses, sorry I got caught up yesterday afternoon. I'll give it a shot today in my script.