views:

20

answers:

1

i want to validate user input for alphanumeric characters and for that im using

             var regex =  /^[a-zA-Z_0-9]$/;
             var asdfsfd = $('#vcr_LinkName').val();
             if (regex.test(asdfsfd)) {
                 alert("true");
             } else {
                 alert("false");                    
             }

but it always go into false condition what im doing wrong

+4  A: 

Your expression would only match one single character.

Try this:

^[a-zA-Z_0-9]+$

Or

^\w+$

regards

Philipp G
If i want to allow space as well what should i do?
Fraz Sundal
Maybe there is a better solution, because this one allows tabs and linebreaks, too. But: ^(\w|\s)+$
Philipp G