这是我的HTML:
<form id="searchForm" action="search.php" method="get">
<input name="q" pattern="[a-zA-Z0-9]{3,}" placeholder="search..." >
<span onClick="searchForm.submit()" class="input-group-addon">
<i class="glyphicon glyphicon-search"></i></span>
</form>
当我点击span时,onClick="searchForm.submit()"
会在不验证模式的情况下提交表单。
我想要的是什么:
当我单击span时,它会检查模式并在模式不适合时显示标准HTML5消息错误,否则它将提交表单。
答案 0 :(得分:0)
将其更改为使用提交按钮或输入类型=“图像”并添加字段所需
<form id="searchForm" action="search.php" method="get">
<input name="q" pattern="[a-zA-Z0-9]{3,}" placeholder="search..." required>
<button type="submit">Submit</button>
</form>
请注意,Safari不支持提交http://caniuse.com/#feat=form-validation
中必填字段的表单验证如上所述,您不需要使用按钮,也可以使用输入类型=“图像”
<form id="searchForm" action="search.php" method="get">
<input name="q" pattern="[a-zA-Z0-9]{3,}" placeholder="search..." required>
<input width="24px" height="24px" type="image" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGYAAABmCAMAAAAOARRQAAAAh1BMVEX///8vLy8yMjIsLCw0NDQ6Ojo3Nzc/Pz8qKio8PDwlJSVBQUEnJyfu7u74+PgiIiIaGhri4uLLy8uPj4+IiIhISEihoaHo6Oh8fHzCwsJNTU23t7dgYGCXl5fX19cYGBixsbFXV1d2dnacnJze3t5oaGhdXV2Dg4MAAADHx8cRERGzs7NMTExiUAPQAAAFIElEQVRoge2Z6ZaiPBCGTYolhEDYRERQ3Ke75/6v78sCqJ+tSKR/Tb8/errPqeGhloRKZTb71a9+9at/SmEQLOL4EMeLIPwxRrKqd1XpsE9A5TH9SoIfgCzync8jzhkjQAjjUeQe6/XEkCBzGWcAgFqJXxmnVjElKMijiLUErKVZhG/Oh6koH1VEQBOsixQKIPJW01RD7nGQDPFoR8q2bflPR2IonaAWwpQSAVEI2/Z6KZggCY949TYnKCLQEIlwfddVEP2LICkQdxdv+nJWFOWHeLTn+KdjURyruaVJrUe8eo+TcoSEKxrieLuvg9gAgsXisDxLkgJJ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