-1

I have variable deleteboxvalue

var deleteboxvalue = "111111111111111";
if(deleteboxvalue.indexOf('0') >= 0) {
    alert("You can't delete all Contact Number.");
return false;
} 
else { 
    alert("All Zeros are not selected."); return true;
}

I want to check if 0 is not exist in this I want to return false and alert as "You can't delete all Contact Number." but in both cases if 0 exist in variable in that case also its returning false and giving me alert as "You can't delete all Contact Number."

2 Answers 2

2

I want to check if 0 is not exist in this I want to return false

If that's the case then you've got your logic reversed. You are currently returning false if 0 is in the string (i.e. it is found at an index greater than or equal to 0). If you want to return false when 0 is not found in the string, you can do this:

if(deleteboxvalue.indexOf('0') == -1) {
    alert("You can't delete all Contact Number.");
    return false;
} 
else { 
    alert("All Zeros are not selected."); 
    return true;
}

However, I may have completely misunderstood what you're trying to do...

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

1 Comment

I meant I have group of boxes for deleting contact number. I have created condition using loop and taken value in variable if checkbox is selected I have added 1 in variable else added 0. FInally I created variable as deleteboxvalue = "111111111111111"; Now I want to check if 0 exist in this I want to return true if 0 doesn't exist I want to return false.
0
  1. Create a function in JavaScript such as:

     function CheckContacts() {
        var deleteboxvalue = "111111111111111";
        if (deleteboxvalue.indexOf('0') >= 0) {
            alert("You can't delete all Contact Number.");
            return false;
        } else {
            alert("All Zeros are not selected."); return true;
        }
    }
    

    and

  2. On body onload call that JavaScript method:

    <body onload="CheckContacts()">
    

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.