我是编程新手,我认为我的问题很容易,但我没有任何想法。如何检查文本字段中的字符数是否匹配11?我试过了,但我觉得这不是一个好线索。
<form name="formz">
<input type='text' name='numbere' id="p" maxlength="11">
</form>
<button onclick="writee()">x</button>
<script>
function writee(){
win=window.open(" ","tab","width=300,height=350")
win.document.open()
var l = document.forms["formz"].numbere;
if(l.length==11){
win.document.write( "under 11 characters <br/>");
}else{
win.document.write( " 11 characters <br/>");
}
}
将minLength="11"
添加到相同的输入
注意,如果您需要更安全的验证,您还必须检查服务器端的值。Html属性只适用于客户端验证。这意味着想要绕过长度要求的人可以在他们的浏览器中编辑源代码,从而绕过您的限制。对于对安全性不敏感的更随意的用例,HTML属性应该足够了。
在HTML方面,请看看输入的属性:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input
在JavaScript中,它的答案如下:Form Validation香草JS为了进一步了解Regex验证,这里有一些例子:https://regex101.com/library?orderBy=RELEVANCE&页面= 1,搜索=验证