Hey.

The following function counts the number of characters entered in the field:

function validateComments(input) { if (input.value.length < 15) { input.setCustomValidity("Π”Π»ΠΈΠ½Π° примСчания Π½Π΅ Π΄ΠΎΠ»ΠΆΠ½Π° ΡΠΎΡΡ‚Π°Π²Π»ΡΡ‚ΡŒ ΠΌΠ΅Π½Π΅Π΅ 15 символов."); } else { // Π”Π»ΠΈΠ½Π° коммСнтария ΠΎΡ‚Π²Π΅Ρ‡Π°Π΅Ρ‚ Ρ‚Ρ€Π΅Π±ΠΎΠ²Π°Π½ΠΈΡŽ, // поэтому ΠΎΡ‡ΠΈΡ‰Π°Π΅ΠΌ сообщСниС ΠΎΠ± ошибкС input.setCustomValidity(""); } } 

But it also takes into account spaces, how can this be removed? Please, show. )

  • There is no such word "comp." Spaces should not be removed, they should not be considered. In addition to spaces, there are a number of non-printable characters, from which you can completely make a comment for you, and you will accept it. - kostyanet

2 answers 2

 input.value.replace(/\s+/g, '') 

Must remove all spaces in the string.

    @Alextrue , note that the replace function does not change the original string, but returns a new one.

     function fn (input) { var l = input.value.replace(/^\s*|\s*$/g, '').replace(/\s+/, ' '); // оставляСм ΠΏΠΎ ΠΏΡ€ΠΎΠ±Π΅Π»Ρƒ ΠΌΠ΅ΠΆΠ΄Ρƒ словами, Ссли Π½Π°Π΄ΠΎ. ... }