Rod
Rod

Reputation: 15457

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?

    <asp:TextBox ID="tbDate" runat="server"></asp:TextBox>
    <asp:TextBox ID="tbHour" runat="server"></asp:TextBox>
    <asp:TextBox ID="tbMinutes" runat="server"></asp:TextBox>

<asp:CustomValidator ID="cvDateControlValidator" runat="server" ErrorMessage="Invalid Date"
    ValidateEmptyText="True" ClientValidationFunction="validateDateOnClient" ControlToValidate="tbDate"
    Display="Dynamic"></asp:CustomValidator>

<script type="text/javascript">   
    function validateDateOnClient(sender, args) {
        if (args.Value.length > 0)
            args.IsValid = false;

        return args.IsValid;
    }

</script>

One suggestion was:

if (tbDate.value != '' || tbHour.value != '' || tbMinutes.value != '')

I want to make sure tbDate, tbHour, tbMinutes together is greater than blank before I perform the client-side validation.

Upvotes: 1

Views: 4024

Answers (5)

Deepakmahajan
Deepakmahajan

Reputation: 866

Try this:

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

}

Upvotes: 0

Jason
Jason

Reputation: 3960

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

Upvotes: 0

Arion
Arion

Reputation: 31239

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:

function validateDateOnClient(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.

Upvotes: 1

DOK
DOK

Reputation: 32831

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)

Upvotes: 2

user1017882
user1017882

Reputation:

document.getElementById('<%=tbDate.ClientID%>').value

...will give you access to the Text property on client-side - is this what you're after?

Using this you can then obviously perform any sort of checks on the string retrieved.

EDIT: I should note that I made an assumption here that you were, in fact, familiar with asp validators as you've used them already - so I proposed a javascript solution to your problem. I would, however, advise you go with the requiredfieldvalidators.

Upvotes: 0

Related Questions