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.
Locked
User avatar
kerbob
Posts: 25
Joined: Sun Mar 03, 2013 12:39 pm

Validate text box content before enabling "Submit" button.

Post by kerbob » Fri Sep 09, 2016 7:36 pm

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: 13992
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Fri Sep 09, 2016 8:10 pm

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
Joined: Wed Oct 08, 2014 6:02 am

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

Post by dan.potter » Sat Sep 10, 2016 4:19 am

Does the masked textbox work with char count?

User avatar
kerbob
Posts: 25
Joined: Sun Mar 03, 2013 12:39 pm

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

Post by kerbob » Sun Sep 11, 2016 1:18 pm

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: 13992
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Sun Sep 11, 2016 1:54 pm

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
kerbob
Posts: 25
Joined: Sun Mar 03, 2013 12:39 pm

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

Post by kerbob » Sun Sep 11, 2016 6:37 pm

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: 13992
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Sun Sep 11, 2016 11:35 pm

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.

Locked