Validate text box content before enabling "Submit" button.

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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.
This topic is 7 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
jpbobrek
Posts: 25
Last visit: Sat Dec 23, 2023 1:52 pm

Validate text box content before enabling "Submit" button.

Post by jpbobrek »

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?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Validate text box content before enabling "Submit" button.

Post by jvierra »

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.
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: Validate text box content before enabling "Submit" button.

Post by dan.potter »

Does the masked textbox work with char count?
User avatar
jpbobrek
Posts: 25
Last visit: Sat Dec 23, 2023 1:52 pm

Re: Validate text box content before enabling "Submit" button.

Post by jpbobrek »

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()
}
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Validate text box content before enabling "Submit" button.

Post by jvierra »

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.
User avatar
jpbobrek
Posts: 25
Last visit: Sat Dec 23, 2023 1:52 pm

Re: Validate text box content before enabling "Submit" button.

Post by jpbobrek »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Validate text box content before enabling "Submit" button.

Post by jvierra »

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.
This topic is 7 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked