Search code examples
javascriptconditional-statementscomparisonboolean-logicdemorgans-law

Disable button based on string length range


I have an input field and a button. The button should be enabled on the following simple scenarios:

  • Input length is equal to 10, or
  • Input length is greater than 15 (should be disabled for 11 to 14).

I tried !str.length = 10 || !str.length >= 15, but these conditions fail, as there is a conflict between both cases. I know I can check whether the length is not equal to 11, 12, 13, or 14, but that doesn't look good. Any better solution will be appreciated.


Solution

  • You used the single equal sign = in your first condition, which is used for setting variables, not checking what they are. It needs to either be == or ===, and in this case, === is preferred, since == is used to check between two of different types (e.g. string, integer).

    Edit: Also, being somewhat new, I couldn't tell what the use of the exclamation marks before the first operands in your conditions were supposed to be, and therefore just copied that part, not knowing what the correct form of what you were trying to accomplish was, since it did not appear that you were trying to reverse the return value. Upon further investigation, I've come to believe that they were just not meant to be there. I've therefore edited my solution below by omitting them entirely.


    Should be:

    (str.length === 10 || str.length >= 15)
    

    Edit: It seems necessary to state that, despite the OP saying it should be enabled if equal to 10 or "greater than 15," the OP clearly means equal to or greater than 15, because the OP indicates three times that this is what they mean by saying "(should be disabled for 11 to 14)," putting ">= 15," and again saying, "not equal to 11, 12, 13, or 14."