atwellpub
atwellpub

Reputation: 6010

Allow only letters; no punctuation no numbers

Hey guys can you help me with this. I've got this '/[^A-Za-z]/' but cannot figure out the punctuations part.

Gracious!

Upvotes: 0

Views: 5310

Answers (5)

iroel
iroel

Reputation: 1798

#^[^a-z]+$#i

Your code was correct, you just need ^ and $. So it means all character from the beginning to the end doesn't allow outside alphabet. Negative match is preferred than positive match here.

Upvotes: 2

TCCV
TCCV

Reputation: 3182

you can also use the shorthand \w for a "word character" (alphanumeric plus _). Of course some regex engines may differ on support for this, but if it's PCRE it should work. See here (under heading "escape sequences").

Upvotes: 0

avpaderno
avpaderno

Reputation: 29669

The regular expression you are using doesn't allow letters; it's the opposite of what you are reported in the title.

/[a-z]/i is enough, if you want to accept only letters. If you want to allow letters like à, è, or ç, then you should expand the regular expression; /[\p{L}]/ui should work with all the Unicode letters.

Upvotes: 4

mctom987
mctom987

Reputation: 892

Inside of a character class, the ^ means not. So you're looking for not a letter. You want something like

[A-Za-z]+

Upvotes: 0

kurreltheraven
kurreltheraven

Reputation: 116

/[^A-Za-z]*/ will match everything except letters. You shouldn't need to specify numbers or punctuation.

Upvotes: 1

Related Questions