field check form for correct filling. the field can contain only Russian words of a space and hyphens

  • there was such an option /^[а-яё]*$/i but swears at spaces

  • this option /^[А-Яа-яё\s]/ui does not swear, but accepts a line if it does not begin with Russian characters and is no longer sensitive to the layout

    2 answers 2

    Use

     /^[а-яё\s-]+$/i 

    or

     /^[а-яА-ЯёЁ\s-]+$/ 

    Details

    • ^ - beginning of line
    • [а-яё\s-]+ - one or more Russian letters, hyphens or whitespace characters
    • $ - end of line

    JS-demo:

     var strs = ['Абв', 'А бв', 'Абв-г', 'Абв1']; var rx = /^[а-яё\s-]+$/i; for (var s of strs) { console.log(s, "=>", rx.test(s)); } 

    • How can I include a hyphen here? checking on names and there are often Bezstuzhivy-Ryumin, Petrov-Vodkina and Muravyovy-Apostles - Evgeny Shevtsov
    • @ YevgenyShevtsov Depending on your requirements for the template. If all the same where this hyphen should be, then simpler than simple - /^[а-яА-ЯёЁ\s-]+$/ . And you can also /^[а-яА-ЯёЁ]+(?:[\s-][а-яА-ЯёЁ]+)*$/ - Wiktor Stribiżew

    try /^[А-Яа-яё\s]{1,}/ui

    • No, the example is not suitable here; kvrtvtbt skips - Evgeny Shevtsov