0
if($("#Prefix").val().trim()=="" && $("#Infix").val().trim()==""){ return false; } 

In the above code, when the element id Prefix or Infix does not exist, it's throwing undefined error

TypeError: $(...).val(...) is undefined 

I know, this can be avoided by checking its length $("#Prefix").lenght>0 and $("#Infix").lenght>0.

My question here is, how can we do both checks inside single if statement itself.

1

3 Answers 3

1

try below code . check this link explain element length condition

 if(($("#Prefix").length && $.trim($("#Prefix").val()) == "") && ($("#Infix").length && $.trim($("#Infix").val())=="")){ return false; } 
Sign up to request clarification or add additional context in comments.

3 Comments

Here you adding one more if statement, I want to achieve it inside the same if statement.
The first if statement can be if( $("#Prefix, #Infix").length > 1 )
@SandeepPatange only length return 0 or more . if 0 then it become false so .length and .length similar. as per my knowledge you need to check this 4 condition due to two level validation
1
if (($("#Infix").lenght>0 && $("#Prefix").lenght>0) && ($("#Prefix").val().trim()=="" && $("#Infix").val().trim()=="")){ //your code here } 

Comments

0

Yes you can.


if statement with && operator stops checking farther when first 0 is returned.
[0 && anything == 0].


So just check for the .length first.

if ( ($("#Infix").lenght && $("#Prefix").lenght) && (another conditions) ) { ... } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.