Checking the form for English characters, if only English characters are entered, then true, if something other than English characters is false. How to fix what would work correctly

"gggg"> true

"pppt"> false

"arff"> true

var a = document.getElementById('form-n'); a.onkeyup = function (e) { var d = a.value; var r = /[az]/gi; console.log(d) if (!r.test(d)) { console.log("false") } else { console.log("true") } } 
  • @AlekseyShimansky, obviously: why pppp false for pppp - Grundy
  • @ Alexey Shimansky fixed the question - Roman Fedorov
  • But after the correction, I did not understand the question :-) - Grundy
  • .updated the answer. - Grundy

1 answer 1

This is all because the regular expression checks only English letters, and the line contains something else, in this case Russian р , which do not resemble this expression.

Why does this not work for the арff line, in which the second is Russian?

Because the given expression searches for at least one character in the string that satisfies the expression.

In order to show that in addition to these characters in the string there should be nothing, you need to change the expression as follows

 /^[az]*$/i 

indicating that the entire string is checked

 ["gggg", "рррр", "арff"].forEach(function(el) { console.log(el, /^[az]*$/i.test(el)); });