tags:

views:

30

answers:

2

Hello all,

I need to define a pattern so that the input string can only contain 0-9, a-z, and A-Z. Also, the length is in the range of 1-30.

Here is the script:

$fileBaseName = 'abc12345';

if (!preg_match("/^[a-zA-Z0-9]{1, 30}$/", $fileBaseName)) {
    echo '<br/>' . 'invalid' . '<br/>';
} else {
    echo '<br/>' . 'valid' . '<br/>';
}

However, when I run this code, the return always is 'invalid'.

How to fix this issue?

Thank you

+4  A: 

You need to drop the space:

if (!preg_match("/^[a-zA-Z0-9]{1,30}$/", $fileBaseName)) {
Matthew Flaschen
Hello Matt,Just see your rep score:)Are you a professional web developer?Thank you
q0987
+2  A: 

This is the regex you're looking for:

/^[a-zA-Z0-9]{1,30}$/
Eton B.