-1

I want to create a regular expression that allows special characters but does not allow non-english letters. My regular expression for my validations is:

/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$/; 

Also this regular expression allows numbers as you can see

RamAlx
  • 6,976
  • 23
  • 58
  • 106
  • My instructions is to use a regular expression for: o The password should be at least eight (8) characters long. o It should consist of lowercase and uppercase Latin alphabetic characters, numbers and special symbols. – RamAlx May 25 '17 at 13:03
  • With my regex if i add a greek A for example no error is showing – RamAlx May 25 '17 at 13:07
  • General tip: You don't have to use regular expressions for everything. For instance, a length enforcement can simply be done by checking the length of the string. You can also perform multiple tests (with simpler regular expressions) instead of trying to cram all the rules into one monolithic regular expression. The resultant code will be more human-readable. And that's usually what counts the most. – Ates Goral May 25 '17 at 13:09
  • I strongly agree with you..but it's mandatory for my project to use regex – RamAlx May 25 '17 at 13:15
  • Please post some examples of what you want to match. – Pedro Lobito May 25 '17 at 13:21
  • Of course suppose i enter this: Αζ11111111! This must not be allow. ζ is a greek character. If i enter this Az11111111! it is correct – RamAlx May 25 '17 at 13:44
  • I don't think I exactly understand the requirement. Are you saying the regex should (1) accept strings containing only English characters, (2) reject everything else? Also, you need to define your set of allowed characters more stringently. E.g. the set of Ascii characters. – kgf3JfUtW May 25 '17 at 15:39
  • `[ -~]` matches all ASCII characters from the space to the tilde. (from https://stackoverflow.com/questions/3203190/regex-any-ascii-character) – kgf3JfUtW May 25 '17 at 15:43

1 Answers1

0

This matches strings not including english-letters:

^[^a-zA-Z]+$

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
Kang Andrew
  • 330
  • 2
  • 14
  • 1
    ***"but does not allow non-english***" – Pedro Lobito May 25 '17 at 13:22
  • ^(?:[#?!@$%^&*-]|[0-9])+$ Then why don't you use that? Note that you need to find out all symbols and put them all in there. If some symbols make errors, you just change them into unicodes such as ^(?:[\u0022\u0020...]|[0-9])+$ – Kang Andrew May 25 '17 at 13:25
  • ^(?:[\u0023\u003F\U0021\u0021\u0040\u0024\u0025\u005E\u0026\u002A\u002D]|[0-9]|[a-zA-Z]){8,}$ – Kang Andrew May 25 '17 at 13:37
  • Before applying the regex, apply trim() function to the variable. And if \u0023\u003F <-- this does not work, add \ at the start like this \\u0023\\u003F. This is because the usage of a regex is slightly different according to what languages the regex is used. – Kang Andrew May 25 '17 at 13:38