我尝试创建表单,并通过JS以编程方式设置模式和标题字段。有人可以帮我解决为什么这不起作用。 下面的代码导致无法在表单字段中输入任何内容,弹出错误说"匹配请求的格式"。
var node = document.getElementById("text");
node.setAttribute("pattern", "\d+");
node.setAttribute("title", "Six or more characters");

<p>A form with a password field that must contain a positive integer:</p>
<form action="/action_page.php">
Enter a number: <input type="text" id="text" name="text">
<input type="submit">
</form>
&#13;
答案 0 :(得分:1)
".validate": "newData.isString() &&
newData.val().matches(/^[\w\][\s]([0-3][0-9])[,\s](19|20)[0-9][0-9]$/)"
是转义字符。如果你想写一个\
,你将不得不用\
\\
var node = document.getElementById("text");
node.setAttribute("pattern", "\\d+");
node.setAttribute("title", "Six or more characters");
node.setAttribute("oninvalid", "setCustomValidity('Six or more characters')");
// this line is needed to clear the error status onchange, so user can enter newly correct input
node.setAttribute("onchange", "try{setCustomValidity('')}catch(e){}");