0

I'm trying to write Javascript code for firstname and lastname validation that contains only a Hebrew characters without numbers or any symbols or any Characters from another language.

I tried to do it and it didn't work that well, The Language is Hebrew, I would like to ensure that the values contain only Hebrew characters, and it's not working that good. It must the user to put Hebrew Characters but if he adds Hebrew Characters he can add numbers or more things and I dont want it to be like that, I want it to be only Characters.

              if (fname1.value == "") {
                  alert("אנא מלא שם פרטי");
                  return false;
              }
              if (fname1.value.length < 2) {
                  alert("שם פרטי חייב להיות גדול מאות אחת ");
                  return false;
              }
              if (fname1.value.length > 12) {
                  alert("שם פרטי חייב להיות קטן מ - 12 אותיות");
                  return false;
              }
              if (!(fname1.value >= 'א' && fname1.value <= 'ת' )){
                  alert("שם פרטי חייב להיות באותיות בעברית ובלי מספרים!");
                  return false;
              }
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Roie Hemo
  • 1
  • 1
  • 1
    https://stackoverflow.com/questions/14313183/javascript-regex-how-do-i-check-if-the-string-is-ascii-only you'd want to check the unicode range of the characters for Hebrew – Robert Mennell Sep 14 '19 at 16:18
  • 1
    You will have to use a regex constraint for the unicode code points that define the hebrew characters. This is not trivial, if you are a beginner this is a difficult place to start. – Jared Smith Sep 14 '19 at 16:18
  • I know the range of the characters for hebrew – Roie Hemo Sep 14 '19 at 16:19
  • 1
    `var fname1 = "״׳-תשרקצץפףעסנןמםלכךיטחזוהדגבא"; for(let i = 0;i=1524){ break; alert("only Hebrew characters please"); } }` – alessandrio Sep 14 '19 at 16:53

1 Answers1

0

This is what I cooked up: const onlyHebrewPattern = new RegExp(/^[\u0590-\u05FF]+$/i);

Inspired from here: https://gist.github.com/YaronMiro/248ed7b6a3113ff1fa3b1cffc5545a9f

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175