13

REGEXで名前/パスなどを確認する必要がある(そして希望する)登録システムを作成しています。これまでのところ、次のようになっています。

//Check so numbers aren't first, such as 00foobar
preg_match('/^(?!\d)[a-z0-9]+$/iD',$usrname);
//Just simple check
preg_match('/^[a-zA-Z0-9]+$/',$psword);

しかし、私は次のようなIFステートメントで愚かなことをしなければなりません。

if strlen($psword) > 30 || if (strlen($psword) < 4) ....

2つの元の正規表現ステートメントで長さチェックをどのように暗示しますか?これは私をとても幸せにするでしょう。

4

2 に答える 2

21

same but using the \w and \d for word and digits, but you might want also to include basic symbols like %!?/ ... etc...

preg_match('/^[\w\d]{4,30}$/',$psword);

the {n,v} would validate for minimum n and maximum v elements before.

like A{2,3} would validate for AA and AAA. you can take a look there for more references

On the same fashion if you want only to set the minimum of patern {n,} would do it. For example:

preg_match('/^[\w\d]{4,}$/',$psword);
于 2010-02-07T02:11:35.263 に答える
5

これでうまくいくはずだと思います:

preg_match('/^[a-zA-Z0-9]{4,30}$/',$psword);

于 2010-02-07T01:56:29.603 に答える