0

I have this jquery code

if (($("#RdbDateRange").is(':checked'))
        && (!$("#CldrFrom").val())
        && (!$("#CldrTo").val())) {}else{}

RdbDateRange is ID for radio button. CldrFrom is ID for input text CldrTo is ID for input text.

I want to do something when the radio is selected and both of the inputs not empty.

but that code go inside the if even when the inputs empty.

why please?

1
  • Empty strings are definitely false in a boolean context. Try applying $.trim() to filter out whitespace. Note that you do not have to explicitly test against the empty string or use length, as all the answers you received so far would like you to believe. Commented Apr 15, 2014 at 12:28

6 Answers 6

2

Use .length property, For spaces You can use $.trim()

if ($("#RdbDateRange").is(':checked')
    && $.trim($("#CldrFrom").val()).length
    && $.trim($("#CldrTo").val()).length){}else{}

OR,

You can also use .trim(), Note: this function is supported in IE9+

if ($("#RdbDateRange").is(':checked')
    && $("#CldrFrom").val().trim().length
    && $("#CldrTo").val().trim().length){}else{}

EDIT

As per requirement the radio is selected and both of the inputs not empty

You don't need !, thus remove it

Sign up to request clarification or add additional context in comments.

5 Comments

maybe the input has spaces, just spaces. ?
maybe is there a trim function ?
can u use the trim please?
it still goes into the if
@MarcoDinatsoli, I have updated answer you dont need !
0

.val() doesnt return true/false. You should check for .val()length == 0 instead of !val()

if (($("#RdbDateRange").is(':checked'))
        && (!$.trim($("#CldrFrom").val()).length == 0)
        && (!$.trim($("#CldrTo").val()).lenght == 0)) {}else{}

2 Comments

so what should I do please?
maybe it is just spaces. I mean whites spaces without actually data
0

Replace !$("#CldrFrom").val() with if($("#CldrFrom").val().trim() !="")

1 Comment

it still goes into the if
0

You must compare in the if clause

 if (($("#RdbDateRange").is(':checked'))
    && (!$("#CldrFrom").val()=="something")
    && (!$("#CldrTo").val()!="")) {}else{}

Comments

0

Try this:

&& (!$("#CldrTo").val().length)) {
//Text is not empty....code here...
}

Comments

0

Try this

if (($("#RdbDateRange").is(':checked'))
        && ($("#CldrFrom").val() !='')
        && ($("#CldrTo").val() !='')) {}else{}

DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.