1

In Javascript, I need to allow alphabets of all (or preferrably accented characters and chinese) languages, hyphen and underscore. I need to disallow special characters and numerals. I looked at many answers but couldn't find anything that matches my needs.

Allowed patterns:

ab_c
åb-ç
的å

Disallowed patterns

å!_
å123
!_!

Please share with me if you have a snippet.. I am on a deadline and freaking out :(

Edit: The following regex matches all non-English characters. If I can alphabets and hypehn and underscore, it would be complete

/[^\u0000-\u007F]/.test("ji");

I got this snippet from this link: https://stackoverflow.com/a/46413244

I added alphabtes, -, _ like below but it fails. Can anyone help?

/[^a-z\_\-\u0000-\u007F]/.test("ji");

1 Answers1

0

Well, the regex you gave at the end of the answer incorrectly excludes _ and -. The same thing that allows _ and - is /[^a-z\u0000-\u007F]|[_-]/

Edit: But what you really want is: /[^\u0000-\u007F]|[a-zA-Z-_]/

Or manually exclude all the special characters you want to: /[^(0-9!?@#$%^&*()+\\=[\]{};':"|,.<>/]/

Sean AH
  • 486
  • 4
  • 8
  • Hi Thanks `/[^a-z\u0000-\u007F]|[_-]/` this fails when we enter `a` as input. Also it allows special characters. Can you help me fix that? –  Oct 26 '21 at 05:37
  • 1
    Ok. How is that? – Sean AH Oct 26 '21 at 05:49
  • Your edited answer works! `\u0000-\u007F1` allowed special chars so I did like this: `/[^\u0000-\u007F]|[a-zA-Z-_]/.test(x) && ! ([^(0-9!?@#$%^&*()+\\=[\]{};':"|,.<>/]/.test(x))` It allows other special chars (eg `£` ) but it was good enough for me.. Thanks –  Oct 26 '21 at 06:49
  • 1
    Ok can you mark it as the accepted answer – Sean AH Oct 26 '21 at 13:18