Validate 2 Text Boxes

Use this forum to ask questions after your subscription maintenance expires or before you buy. Need information on licensing or pricing? Questions about a trial version? This is the right place for you. No scripting questions, please.
Forum rules
DO NOT POST SUBSCRIPTION NUMBERS, LICENSE KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.
This topic is 7 years and 2 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.
User avatar
merhic
Posts: 4
Last visit: Wed Dec 28, 2016 9:11 am

Validate 2 Text Boxes

Post by merhic »

Product, version and build: PowerShell Studio 2016 v5.3.131
32 or 64 bit version of product: 64 bit
Operating system: Windows 10 Enterprise 1607
32 or 64 bit OS: 64 bit

Disclaimer: Complete Newb so please be kind.

I am trying to make the following POSH into a GUI that looks like the attached screenshot. Basically I need to prompt the user to enter a PIN that is minimum 6 numerical characters and max 20 and to make sure that their entries match in both text boxes or else tell them what the issue is (not matching and/or not meeting limit). I don't want the text to be masked so the user can see what they put in. This is for the purpose of setting BitLocker and Activating it with TPM and PIN. I am stuck in a loop that is crashing the form when I port it into PowerShell Studio 2016. Submit button should not appear until PIN matches criteria.
POSH:

Do
{
    $Pin = Read-Host "PIN must be between 6-20 characters and contain only numbers. Please enter a PIN number"
} While (($Pin.Length -lt 6) -or ($Pin.Length -gt 20) -or ($Pin -match "\D"))

Do
{
    $Pin2 = Read-Host "Please confirm the PIN number"
    $Match = $Pin.CompareTo($Pin2)

    If ($Match -ne 0)
    {
        Write-Host "PIN does not match"
        $runagain = 'y'
    }

    else
    {
        $runagain = 'n'
    }
} While ($runagain -eq 'y')

$SecPIN = ConvertTo-SecureString $pin -AsPlainText -Force
Add-BitLockerKeyProtector -MountPoint "C:" -Pin $SecPIN -TpmAndPinProtector
Backup-BitLockerKeyProtector -MountPoint "C:" -KeyProtectorId $Volume.KeyProtector[0].KeyProtectorId

What I have that is not really working in PowerShell Studio:

$ErrorProvider = New-Object System.Windows.Forms.ErrorProvider
$buttonSubmit.Enabled = $false

$textbox3_TextChanged = {
	$buttonSubmit.Enabled = $textbox3.Text -ne ''
}

$buttonSUBMIT_Click = {
	Do
	{
		$pin = $textbox2.Text
	}
	While (($pin.Length -lt 6) -or ($pin.Length -gt 20) -or ($pin -match "\D"))
	
	Do
	{
		$pin2 = $textbox3.Text
		$Match = $pin.CompareTo($Pin2)
		
		If ($Match -ne 0)
		{
			$ErrorProvider.SetError($textbox2, "ONLY 20 CHARS ALLOWED!"); #InValid
			Write-Host "Not Good"
			$runagain = 'y'
		}
		
		else
		{
			$ErrorProvider.Clear() #Valid
			Write-Host "Good"
			$runagain = 'n'
			$Form.Close()
		}
	}
	While ($runagain -eq 'y')
}

$SecPIN = ConvertTo-SecureString $pin -AsPlainText -Force
Add-BitLockerKeyProtector -MountPoint "C:" -Pin $SecPIN -TpmAndPinProtector
Backup-BitLockerKeyProtector -MountPoint "C:" -KeyProtectorId $Volume.KeyProtector[0].KeyProtectorId
Attachments
DialogBox.PNG
DialogBox.PNG (47.28 KiB) Viewed 6753 times
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Validate 2 Text Boxes

Post by Alexander Riedel »

Ok, two things. Don't write "PIN Number" :D PIN = Personal Identification Number. So "PIN Number" is "Personal Identification Number Number" ;-)

Now for your loop. You do a forever loop in the $buttonSUBMIT_Click handler, never giving the user another chance to enter anything.
  1. $buttonSUBMIT_Click = {
  2.     $pin = $textbox2.Text
  3.     if(($pin.Length -lt 6) -or ($pin.Length -gt 20) -or ($pin -match "\D")) {
  4.         $ErrorProvider.SetError($textbox2, "Invalid PIN!"); #InValid
  5.         Write-Host "Not Good"
  6.         return;
  7.     }
  8.     $pin2 = $textbox3.Text
  9.     $Match = $pin.CompareTo($Pin2)
  10.    
  11.     If ($Match -ne 0)
  12.     {
  13.     $ErrorProvider.SetError($textbox2, "PINs do not match"); #InValid
  14.     Write-Host "Not Good"
  15.     return;
  16.     }
  17.     else
  18.     {
  19.     $ErrorProvider.Clear() #Valid
  20.     Write-Host "Good"
  21.     $Form.Close()
  22.     }
  23. }
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
merhic
Posts: 4
Last visit: Wed Dec 28, 2016 9:11 am

Re: Validate 2 Text Boxes

Post by merhic »

LOL - touché! The irony here is that I am the one who usually polices the misuse of NIC card and ATM machine.

Thank you very much for the quick reply. I will give your code a try and report back.
User avatar
merhic
Posts: 4
Last visit: Wed Dec 28, 2016 9:11 am

Re: Validate 2 Text Boxes

Post by merhic »

Looks like that worked in the sense that it does correctly prompt me to match the PINs or use the proper length, accordingly, but I am getting the following error after I click the Submit button. Do I need to specify the form name or something like that?

ERROR: You cannot call a method on a null-valued expression.
EnableBitLockerPIN.psf (29, 3): ERROR: At Line: 29 char: 3
ERROR: + $Form.Close()
ERROR: + ~~~~~~~~~~~~~
ERROR: + CategoryInfo : InvalidOperation: (:) [], RuntimeException
ERROR: + FullyQualifiedErrorId : InvokeMethodOnNull
ERROR:
User avatar
merhic
Posts: 4
Last visit: Wed Dec 28, 2016 9:11 am

Re: Validate 2 Text Boxes

Post by merhic »

Got it. Needed the actual form name.

$formEnableBitLockerPIN.Close()

Thank you very much, again!
This topic is 7 years and 2 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.