¿Qué falta en esta validación de expresiones regulares?

Whats wrong with this regular expression?

^[a-zA-Z,\d,\&,\#,\-,\(,\']{3,15}$       

Valid input is alphanumeric, space, #, &, ', , - with max and minimum length of 3 and 15 respectively.

when I enter the following valid input, it fails:

MT90#&' ABCD

preguntado el 01 de febrero de 12 a las 04:02

5 Respuestas

You don't need comma separators and you don't need to escape all those characters:

^[a-zA-Z\d\s&#-\(']{3,15}$

Respondido 02 Feb 12, 01:02

Hi Dawood, isnt the \d is for digits? - user1052591

You're right, but you don't need comma separators and you don't need to escape &, # and '. - dawood

You missed space \s, and no need to use , in char set.

This will be ok:

/^[a-zA-Z\d\s&#-\(']{3,15}$/

Actualizar: \s will match on all whitespace (including tabs and line breaks as well as spaces), if you want just space, change to :

/^[a-zA-Z\d &#-\(']{3,15}$/

Respondido 01 Feb 12, 08:02

\s coincidirá en todos whitespace (including tabs and line breaks as well as spaces) which is not technically what the question asked. - Matt Brock

You're missing a space. Try this: [a-zA-Z\d\&\#\-\(\' ]{3,15}$

Respondido 01 Feb 12, 08:02

You only need to escape the following characters:

[ \ ^ $ . | ? * + ( )

You also don't need to have ',' between you characters in the set.
si usas \s in you regex, it will cover space, tab y line breaks.

Respondido 01 Feb 12, 08:02

^[a-zA-Z\d\&\#\-\(\'\ ]{3,15}$ 

remove the commas and you're good to go

Respondido 01 Feb 12, 08:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.