我试图在密码字段上使用此模式,该密码字段显示"密码应为最小6个字符并包含1个非字母字符":
^(?=.{6})(?=.*[^a-zA-Z])
但是,当将其传递到pattern
属性时,它将停止工作。查看此演示:
document.querySelector('form').addEventListener("submit", onFormSubmit);
function onFormSubmit(e) {
e.preventDefault();
var pwEl = document.querySelector('.password');
alert('With pattern: ' + pwEl.checkValidity() + ', without pattern: ' + /^(?=.{6})(?=.*[^a-zA-Z])/.test(pwEl.value));
}

<form novalidate>
<input class="password" type="password" placeholder="Enter password" pattern="^(?=.{6})(?=.*[^a-zA-Z])" />
<button type="submit">submit</button>
</form>
&#13;
尝试输入: