1

RegEx の熱心なユーザーではありません。ただし、ユーザー名フィールドへの入力を確認する最善の方法は、文字 (大文字または小文字)、数字、および _ 文字のみを許可し、サイト ポリシーに従って文字で開始する必要があるものを使用することだと思います。My RegEx とコードは次のとおりです。

var theCheck = /[a-zA-Z]|\d|_$/g;
alert(theCheck.test(theUsername));

いろいろな組み合わせで試してみても、すべて「真」を返しています。

誰でも助けることができますか?

4

3 に答える 3

3

theUsernameあなたの正規表現は、「文字、数字、またはアンダースコアで終わる」と言っています。

代わりにこれを試してください:

var theCheck = /^[a-z]([a-z_\d]*)$/i; // the "i" is "ignore case"

これは、「theUsername文字で始まり、文字、数字、またはアンダースコアのみを含む」ことを示しています。

注: ここで「g」は必要ないと思います。これは「すべての一致」を意味します。文字列全体をテストしたいだけです。

于 2012-05-14T22:56:42.443 に答える
3

このようなものはどうですか:

^([a-zA-Z][a-zA-Z0-9_]{3,})$

パターン全体を説明するには:

^ = Makes sure that the first pattern in brackets is at the beginning
() = puts the entire pattern in a group in case you need to pull it out and not just validate
a-zA-Z0-9_ = matches your character allowances
$ = Makes sure that this must be the entire line
{3,} = Makes sure there are a minimum of 3 characters. 
    You can add a number after the comma for a character limit max
    You could also use a +, which would merely enforce at least one character match the second pattern. A * would not enforce any lengths
于 2012-05-14T22:57:17.290 に答える
1

これを正規表現として使用します。

^[A-Za-z][a-zA-Z0-9_]*$
于 2012-05-14T22:56:13.330 に答える