Share via


Disable button if any of the textboxes is empty

Question

Wednesday, October 2, 2013 8:47 PM

Hi, 

I need to have a button disabled until the user add text on the three textboxes that I have created, currently when the users add text to any of the textboxes the button gets enabled, here is the code that I have now:

$txtcreateuser.add_TextChanged({
if ($txtcreateuser.Text.Length -ne 0) 
    {
        $buttoncreate.Enabled = $true
    }
    else
    {
        $buttoncreate.Enabled = $false
    }

})

$txtlastname.add_TextChanged({
if ($txtlastname.Text.Length -ne 0) 
    {
        $buttoncreate.Enabled = $true
    }
    else
    {
        $buttoncreate.Enabled = $false
    }

})

$txtslastname.add_TextChanged({
if ($txtslastname.Text.Length -ne 0) 
    {
        $buttoncreate.Enabled = $true
    }
    else
    {
        $buttoncreate.Enabled = $false
    }

})

I'll appreciate any suggestions.

All replies (3)

Wednesday, October 2, 2013 8:56 PM âś…Answered

Hi,

I'm going to assume one of those last name sections is actually supposed to be for a first name section. Since you only want to enable the button if all three criteria are met, try using an if statement like this:

If ($txtslastname.Text.Length -ne 0 -and $txtlastname.Text.Length -ne 0 -and $txtcreateuser.Text.Length -ne 0) { ... }

EDIT: Never mind, you do have three distinct sections. Code has been updated to reflect txtslastname, txtlastname, and txtcreateuser.

Don't retire TechNet! - (Maybe there's still a chance for hope, over 12,000+ strong and growing)


Wednesday, October 2, 2013 9:32 PM

Hi Mike,

Thanks for your help, now it is working as I wanted!

Here is a screenshot of what I have:

Where $txtcreateuser was for the "Name" textbox, $txtlastname for "Lastname" textbox  and $txtslastname for "Second Lastname" textbox. Now with your help this is how I have the code that enables the button "Create New User" only if the three textboxes are not empty:

$txtcreateuser.add_TextChanged({
if ($txtslastname.Text.Length -ne 0 -and $txtlastname.Text.Length -ne 0 -and $txtcreateuser.Text.Length -ne 0) 
    {
        $buttoncreate.Enabled = $true
    }
    else
    {
        $buttoncreate.Enabled = $false
    }

})

$txtlastname.add_TextChanged({
if ($txtslastname.Text.Length -ne 0 -and $txtlastname.Text.Length -ne 0 -and $txtcreateuser.Text.Length -ne 0) 
    {
        $buttoncreate.Enabled = $true
    }
    else
    {
        $buttoncreate.Enabled = $false
    }

})

$txtslastname.add_TextChanged({
if ($txtslastname.Text.Length -ne 0 -and $txtlastname.Text.Length -ne 0 -and $txtcreateuser.Text.Length -ne 0) 
    {
        $buttoncreate.Enabled = $true
    }
    else
    {
        $buttoncreate.Enabled = $false
    }

})

Regards.


Wednesday, October 2, 2013 10:52 PM

Cheers, glad I could help out.

Don't retire TechNet! - (Maybe there's still a chance for hope, over 12,000+ strong and growing)