Such a task: the user began typing in Russian, an error appears after entering the first letter, the input is blocked, the entered letter is not deleted:

$('.js-au-email').keyup(function() { var regexp = /^[a-zA-Z0-9-_@\.]+$/i; if(!regexp.test(this.value)) { alert('ошибка в символС') } else { } }); 

Tell me how to further prohibit input, but without blocking the text field, to give the opportunity to delete the character?

  • because you have a search case-insensitive (i), it is not a-zA-Z , but you can simply az and if you need to check for Russian letters, then az - Alex

1 answer 1

Option 1:

 var count_error = 0; $('.js-au-email').on("keyup", function() { var regexp = /^[a-zA-Z0-9-_@\.]+$/i; if (!regexp.test($(this).val()) && count_error == 0) { alert('ошибка в символС'); count_error = 1; } else if (!regexp.test($(this).val()) && count_error == 1 && $(this).val().length > 0) { alert('ошибка в символС'); $(this).val($(this).val().slice(0, -1)); } else if ($(this).val().length < 1 || regexp.test($(this).val())) { count_error = 0; } console.log(count_error); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" name="js-au-email" class="js-au-email"> 

UPD: Added a check for a null value, so that alert does not pop up when deleting the last character in a string that does not match the pattern.

Option 2: if you do not hurry, you can simplify:

 $('.js-au-email').on("keyup", function() { if (($(this).val().match(/[^a-zA-Z0-9-_@\.]/g)||[]).length == 1) { alert('ошибка в символС'); } else if(($(this).val().match(/[^a-zA-Z0-9-_@\.]/g)||[]).length > 1){ alert('ошибка в символС'); $(this).val($(this).val().slice(0, -1)); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" name="js-au-email" class="js-au-email"> 

Option 2.1: if it is necessary to prohibit entry, incl. correct characters, after the entered incorrect character:

 $('.js-au-email').on("keyup", function() { if (($(this).val().match(/[^a-zA-Z0-9-_@\.]/g)||[]).length == 1) { alert('ошибка в символС'); if ($(this).val().length > 1 && ($(this).val().substr(-1).match(/[^a-zA-Z0-9-_@\.]/g) || []).length < 1) { $(this).val($(this).val().slice(0, -1)); } } else if(($(this).val().match(/[^a-zA-Z0-9-_@\.]/g)||[]).length > 1){ alert('ошибка в символС'); $(this).val($(this).val().slice(0, -1)); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" name="js-au-email" class="js-au-email"> 

  • ah if only if - Azamat Sharafutdinov
  • @Azamat Sharafutdinov Do you need to save the first erroneous character? - Alex
  • Yes, it is true, but for yours when nothing works, i.e. even the symbols relating to the regular season do not give anything to enter - Azamat Sharafutdinov
  • @Azamat Sharafutdinov updated the answer, now the first erroneous character is saved, the second one is deleted - Alex
  • one
    I understood everything now :-) - Azamat Sharafutdinov