Does not fulfill part of the condition. That it was clear I will describe the principle in words. A test is displayed in the input if: enable == true (the input is not fully completed), or valid == false (there are no matches for characters). It is necessary that the test be displayed under any of these conditions (and if the imput week is completely filled and if there are no matches).

Here is the part of the code that does for this condition: } else if (enable == true && valid == false ) {

The following error in a similar condition: enable == false && valid == true here while simultaneously maintaining enable == false and valid == true should pop up an alert message. But the event is triggered when 11 digits in the field, and should trigger when 12.

here is the entire condition code:

  var phone = this.value; if (!phone || phone.replace(/\D/g, '').length < 12) enable = false; if (codes[phone.substr(5, 3)]) { valid = true } else if (enable == true && valid == false ) { $('.form-control').val('Некорректный ввод'); } if (enable == false && valid == true) { obj = { dates: 'дата', time: 'время', phone: phone }; obj['date'] = retite alert(obj['date']); } }).end().end() 

Here is the entire code of the example: http://jsfiddle.net/0xL0fctq/35/

    1 answer 1

     if (enable == true && valid == false) alert(1); 

    - the alert will work only if at the same time the first value is true, and the second is false. If you want it to work when one of the conditions is met, use the operator || instead of && .

    But the event is triggered when 11 digits in the field, and should trigger when 12.

     if (!phone || phone.replace(/\D/g, '').length < 12) enable = false; 

    - replace either the strict inequality sign < by <= , or the number 12 by 13.

    • @ Dmitry, but do not tell me why if you remove one digit from the input, then the event will trigger an alert instead of an error in the input? PS I fixed everything as you indicated. - Valeriy1996
    • @ Valeriy1996, and what alert? I hope you didn't copy the first line of my answer to your code? I brought it for clarity. You only need to change the operator there. - Dmitriy
    • @ Dmitry, no, I did as you said. I have an Alert in the code, it works when there are 12 characters in the input, I corrected it in the condition for 13. But when you remove one from the input instead of the condition that causes an error in the input, an alert is triggered which "says that everything is correct" - Valeriy1996
    • @ Dmitry, it turns out when you add characters to the field, it works as it should, and when you delete it, it’s all the other way around, for some reason, he thinks that all the characters are in place, that’s strange ... - Valeriy1996
    • @ Valeriy1996 In the example of the link from your question, the alert does not work with or without my edits. But the message about incorrect input appears with enviable regularity. Alas, I cannot help you here. I am not too friendly with JS, and my answer, as well as your initial question, was of a purely logical nature (I have no problems with that). - Dmitriy