1

I am using this code for a X 'n' O file. What I have done is an IF statement with NOT option but using || operator I want to define multiple choices. That states the problem here. Please help me. Code below:

if (!xslot=="1" || !xslot=="2" || !xslot=="3" || !xslot=="4" || !xslot=="5" || !xslot=="6" || !xslot=="7" || !xslot=="8" || !xslot=="9") {
alert("Please enter a valid slot number from 1 to 9.");
}

Great thanks to anyone who helps me.

0

3 Answers 3

2

You can use indexOf(), it returns the first index at which a given element can be found in the array, or -1 if it is not present.

var arr = [];
for (var i = 1; i < 10; i++) {
  arr.push(i);
}

var xslot = 15;
if (arr.indexOf(xslot) == -1) {
  alert('xslot is not in range')
};

You could also use $.inArray()

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

Comments

2

If you're only checking a number in range, try this snippet:

var i_xslot = parseInt(xslot, 10);
if (i_xslot < 1 || i_xslot > 9) {
  alert("Please enter a valid slot number from 1 to 9.");
}

Comments

1

Since you want to work with numbers, treat the input as a number :

if(+xslot < 1 || +xslot > 9) 
    alert("Please enter a valid slot number from 1 to 9.");

+xslot force the conversion to an int of xslot...

1 Comment

Actually this one works well! Thanks @SamuelCaillerie.

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.