Validate text box content before enabling "Submit" button.
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Validate text box content before enabling "Submit" button.
I'd like to ensure content of a textbox is greater than 16 characters before I allow a user to click a "Submit" button on the form. Is there a way to make the Submit button grayed out until the textbox has at least 16 characters?
Re: Validate text box content before enabling "Submit" button.
You can use the :validating" to test the box for length or you can capture the keystrokes and count the TB length and enabel the button.
- dan.potter
- Posts: 709
- Last visit: Wed Nov 14, 2018 11:39 am
Re: Validate text box content before enabling "Submit" button.
Does the masked textbox work with char count?
Re: Validate text box content before enabling "Submit" button.
I'm still struggling. I need to validate that a textbox has more than 10 characters when they click the 'allow access' button before I allow them to proceed. I couldn't figure out the validating event and tried the below instead. Although I get a correct popup, all the parts of the if loop close the form instead of just the desired 'else' loop.
$buttonAllowAccess_Click={
If ($textbox1.Text.Length -eq 0) {
$OUTPUT = (new-object -ComObject wscript.shell).Popup("Business case required. Field must be at least 10 characters", 0, "Error", 0x0)
}
elseif ($textbox1.Text.Length -le 10) {
$OUTPUT = (new-object -ComObject wscript.shell).Popup("Field must be at least 10 characters.", 0, "Error", 0x0)
}
else {
Write-Host "Allow"
Add-Content -Path $filedebug -Value "[DEBUG] Business Case: $textbox1.Text "
$FirstForm.Hide()
}
}
$buttonAllowAccess_Click={
If ($textbox1.Text.Length -eq 0) {
$OUTPUT = (new-object -ComObject wscript.shell).Popup("Business case required. Field must be at least 10 characters", 0, "Error", 0x0)
}
elseif ($textbox1.Text.Length -le 10) {
$OUTPUT = (new-object -ComObject wscript.shell).Popup("Field must be at least 10 characters.", 0, "Error", 0x0)
}
else {
Write-Host "Allow"
Add-Content -Path $filedebug -Value "[DEBUG] Business Case: $textbox1.Text "
$FirstForm.Hide()
}
}
Re: Validate text box content before enabling "Submit" button.
You have to use the validating event of the textbox to validate the length anytime the textbox losses focus. When the user is in the textbox they will not be able to leave
until they satisfy the requirement.
If you do it in the click event just display the message and do nothing.
$form.Hide() does not work correctly in a dialog.
Read the forum blog posts on how to do validation.
until they satisfy the requirement.
If you do it in the click event just display the message and do nothing.
$form.Hide() does not work correctly in a dialog.
Read the forum blog posts on how to do validation.
Re: Validate text box content before enabling "Submit" button.
jvierra,
Thanks so much for the help! I'm working now. The forum blog helped, but the samples were the best. Things that got me snagged:
By default, the forms textbox did not have focus. I could hit Ok or Cancel and the form would close without validation.
Fix: The TextBox property TabIndex had to be set to 1.
After the TextBox had focus, I could not click the Cancel button unless I made the validation of TextBox successful.
FIX: The Cancel button property "CausesValidation" should be set to False.
Thanks so much for the help! I'm working now. The forum blog helped, but the samples were the best. Things that got me snagged:
By default, the forms textbox did not have focus. I could hit Ok or Cancel and the form would close without validation.
Fix: The TextBox property TabIndex had to be set to 1.
After the TextBox had focus, I could not click the Cancel button unless I made the validation of TextBox successful.
FIX: The Cancel button property "CausesValidation" should be set to False.
Re: Validate text box content before enabling "Submit" button.
That is good. Yes. It take time to learn how to design forms. Unfortualtely there is no comprehensive book on how to design forms with PowerShell. I can only recommend reading the rest of the blog posts on Forms and controls. They will help you understand better how to get things to work easily.
You seem to be well on your way to understanding controls. Keep on PosHing.
You seem to be well on your way to understanding controls. Keep on PosHing.