1

I want to validate a form field by checking of the input contains any letters. All other characters and numbers should be allowed. I'm quite bad at regular expressions, and I can't find a correct solution anywhere.

I've tried this:

/[^A-Za-z]/g

but this only returns false if the string consists of only letters (i.e. 432ad32d should return false as well).

Could anyone tell me how to do this?

Leon
  • 1,999
  • 22
  • 38
  • 2
    Is `é` allowed? ;-) What about `ݰ` or `Ӝ`? – Lucas Trzesniewski Oct 16 '15 at 16:26
  • Good question. No :) I want to use this to validate phone numbers, but since some people use certain characters I would rather not check for numbers only. But, thinking of it, maybe it would be better to check for allowed characters ( 0-9, - , + , ( , ) , and spaces). Length doesn't matter. – Leon Oct 16 '15 at 16:29
  • 3
    That's exactly the conclusion I wanted you to make :) – Lucas Trzesniewski Oct 16 '15 at 16:30
  • Am I right in understanding you'd like to detect if a text value contains letters? I.e. expressed formally, for a string of characters, you want to know if there exists one character in this string that is a letter? For instance: - "aabbcc": contains letters only, do you want `true` or `false`? - "23a": contains letters and digits, do you want true, do you want `true` or `false`? - "123": contains digits only, do you want `true` or `false`? - "&%£": contains symbols only, do you want `true` or `false`? – Thomas Oct 16 '15 at 16:38
  • 1
    Yes, for a very basic phone number validation, I wanted to allow anything but letters. So if the input had one or more letters in it, the regex should return false. As Lucas pointed out however, there are a lot of special "letters" out there, so checking for allowed characters rather then disallowed ones might be a better solution, so I might just go and do that. Thank you though! – Leon Oct 16 '15 at 16:42

5 Answers5

2

Using a whitelist of allowed characters is the best approach in your case:

/^[-+\d(), ]+$/

Unicode has many things it calls a letter, better not mess with that in the first place. And JavaScript regexes aren't well suited for handling these (they lack things like \p{L} for instance unless you use an external library).

Also, by using the whitelist approach you can be sure about the kinds of inputs which will be accepted by your form. You can't predict the kind of mess users could input otherwise. Think about things like this:

TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉ ͠P̯͍̭O̚​N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ

:-)

Community
  • 1
  • 1
Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
2
/[^A-Za-z]/

This regex matches a single non-letter, which isn't very useful. Yura Yakym's answer matches the beginning of the string, any number of non-letters, and then the end of the string, which is useful when it matches: it means your string contains only those things.

Another useful regex is:

/[A-Za-z]/

This matches a single letter, which is useful when it doesn't match: it means your string does not contain any letters at all.

For your question in general, "how can I ensure a string lacks letters?", I would use that second regex: I would try to match a letter, and hopefully fail to do so. For input validation though, I'd prefer a regex that describes all possible valid inputs. If /^[^A-Za-z]*$/ does so, then use that. If you have additional requirements, add those to it. Don't have multiple "no letters? OK. no non-dash special characters? OK." ... well, unless you want to provide error messages precisely about such things.

Julian Fondren
  • 5,459
  • 17
  • 29
1

Try this regular expression: ^[^A-Za-z]*$

Yuriy Yakym
  • 3,616
  • 17
  • 30
  • The `*` operator matches zero or more of an expression. So it would match an empty string. I think what you mean is `+` – Richard Hamilton Oct 16 '15 at 16:33
  • The question was to check if string doesn't include any letters. So empty string has no letters. It's for form validation. So, maybe the need is to allow empty string? – Yuriy Yakym Oct 16 '15 at 16:36
0

You need to include anchors

/^[^A-Za-z]+$/g

This will ensure the string starts and ends with one or more numbers/special characters

Richard Hamilton
  • 25,478
  • 10
  • 60
  • 87
0

You forgot about start and end markers. Also you don't need g flag.

/^[^A-Za-z]*$/

Anyway, that's strange as I can enter ciryllic letters still.

Qwertiy
  • 19,681
  • 15
  • 61
  • 128