Skip to content Skip to sidebar Skip to footer

Best Way To Check If 3 Textboxes Are Empty

I have 3 textboxes and I want to check if put together they all add up to greater than blank. What's the best way to accomplish that?

Solution 1:

I think you can do this with a single CustomFieldValidator.

I think you are very close to the answer on your own. I would sum the lengths like this:

if (tbDate.value.length + tbHour.value.length + tbMinutes.value.length > 0)

Solution 2:

I would use the RequiredFieldValidator

<asp:RequiredFieldValidator id="RequiredFieldValidator2"
                    ControlToValidate="yourTextBox"
                    Display="Static"
                    ErrorMessage="*"
                    runat="server"/> 

and then have one Validator per textbox. Because you do not need any javascript. So you do not need to do the work on many pages that a control does.

See here for more information

Edit

Or you can do it with JQuery. Something like this:

functionvalidateDateOnClient(sender, args) {
        $('input[type=text]').each( function() {
          if(($this).val().length==0) {
             args.IsValid = false;
          }
     });

        return args.IsValid;
    }

This will loop all the text boxes on the page.

Solution 3:

If you're using .NET 4 you could do this

(!string.IsNullOrWhiteSpace(tbDate.Text) || !string.IsNullOrWhiteSpace(tbHour.Text)
|| !string.IsNullOrWhiteSpace(tbMinutes.Text))

With earlier versions you could do

(tbDate.Text.Trim().Length >0|| tbHour.Text.Trim().Length >0||
tbMinutes.Text.Trim().Length >0)

That way would know if you have just a bunch of blank spaces

Solution 4:

Try this:

if(tbDate.value > 0 || tbHour.value > 0 || tbMinutes.value > 0)
{

}

Post a Comment for "Best Way To Check If 3 Textboxes Are Empty"