var must contain

how do i say that a variable must contain
only the characters a through z or A through Z or 0 to 9

ie legal
Test
test
test1
test2

illegal
test 1
test+1

i tried
 
if(m/a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|0|1|2|3|4|5|6|7|8|9/){  
but it wont work
boofullsAsked:
Who is Participating?
 
bigbedCommented:
if(/^[a-zA-Z0-9]*$/)

if you would allow underscore (_), then
use:
if (/^\w*$/)

0
 
ozoCommented:
#or
if( /[\W_]/ ){ illegal }else{ legal }
0
 
boofullsAuthor Commented:
both good but bigbed got there first
sorry ozo :(
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.