views:

50

answers:

2

Hi,

Quick questio : I need to allow an input to only accept letters, from a-z to A-Z, but can't find any expression for that, using javascript test() method.

Cheers!

+5  A: 
/^[a-zA-Z]+$/.test('sfjd')

Note: If you have any punctuation marks or anything, those are all invalid too. Dashes and underscores are invalid. \w covers a-zA-Z and some other word characters. It all depends on what you need specifically.

meder
do you know if there is much of a difference between regex.test(string) and string.match(regex)?
halkeye
hmm it seems I was missing only the start and end tags, thanks!
yoda
A: 

There is a predefined class for this: /\w+/ (that is one or more word characters)

(just checked on java documentation: this allows also digits so maybe you don't want to use it)

Jack