HTML5输入模式验证字母和数字

时间:2018-05-02 10:50:28

标签: html regex validation

我需要使用以下要求验证密码:

至少1封信, 至少1个号码, 最少6个字符和最多12个字符, 不允许使用特殊字符。 这是我到目前为止所拥有的。

<form>
Password: <input type="password" name="pw" pattern="^(?=.*[a-zA-Z])(?=.*[0-9]).{6,12}$" title="Must contain at least one number and one letter, and at least 6 to 12 characters (special characters not allowed)">
<input type="submit">
</form>

1 个答案:

答案 0 :(得分:1)

以下是解决您问题的正则表达式。

^(?=.*[a-zA-Z])(?=\w*[0-9])\w{6,12}$

完整的答案是:

<form>
    Password: <input type="password" name="pw" pattern="^(?=.*[a-zA-Z])(?=\w*[0-9])\w{6,12}$" title="Must contain at least one number and one letter, and at least 6 to 12 characters (special characters not allowed)">
    <input type="submit">
</form>