1

I have a form which has three blocks face-book, twitter and pinterest. I added a condition to allow submitting the form only when one of the block is selected. For that I did:

if ($("#fb").prop("checked") || $("#tw").prop("checked")||
  $("#pin").prop("checked")) {
    ...
    ...
}
if ($("#fb").prop("checked") || $("#tw").prop("checked")||
  $("#pin").prop("checked")) {
    ...
    ...
}

Now I want to add another condition where it will check inside the twitter block to check if the text field has less than 140 characters. For that I added an and condition

if ($("#fb").prop("checked") || $("#tw").prop("checked") && $(#"tw_text").val().length <= 140||
  $("#pin").prop("checked")) {
    ...
    ...
}

But this has some faults like if the facebook is selected and tw_text has more than 140 characters then also the form gets submitted.

Please guide me to the correct way to add conditions to prevent submitting the form.

1
  • can you please edit the question and explain the exact logic (but do keep in the non-functioning code). Do feel free to use psuedo-code and plenty of parenthesis to make it clear. Commented May 17, 2016 at 17:28

3 Answers 3

4

You need parentheses to group them (the precedence of logical OR || is smaller than logical AND &&):

if ($("#fb").prop("checked") || 
    ($("#tw").prop("checked") && $(#"tw_text").val().length <= 140) ||
    $("#pin").prop("checked")) {
...
Sign up to request clarification or add additional context in comments.

10 Comments

But if user selects fb and tw and no of twitter text's character is more than 140 , then also it will submit the form. i want to prevent this.
so you want only one (valid) selection?
No. i want user to select minimum one of the three and if he selects twitter he should have twitter text less than 140. If fb and twitter are selected together , it should also check for the no of characters and prevent submitting if condition gets false.
you answer is missleading, so if twitter and twitter length <140 and facebook then ...? yes or no?
Then it should allow submitting the form.
|
0

From what I understand from your description is that only for twitter you need a compulsory condition to be satisfied. Simply using brackets should help you here. Hope the following helps: if ($("#fb").prop("checked") || ($("#tw").prop("checked") && $(#"tw_text").val().length <= 140) || $("#pin").prop("checked")) { ... ... }

1 Comment

But if user selects fb and tw and no of twitter text's character is more than 140 , then also it will submit the form. i want to prevent this.
0

Solution: I used this way to resolve the issue.

if($("#fb").prop("checked") || $("#tw").prop("checked") || $("#pin").prop("checked")) { 
    if ($("#tw").prop("checked")) { 
        if($('#tw_text').val().length <= 140) { 
           ...submit form... 
        } 
        else { 
            .... show error for twitter text length.... 
        } 
    } 
    else { 
        ...submit form... 
    } 
} 
else { 
    ... show error message to select minimum one of the options between fb,tw and pinterest ... 
}

1 Comment

@brianlmerritt Thank you for the concern. I did solve the issue using this method.

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.