我想让输入数字是6位或9位数字。不少于6位数,不小于6位至9位数,也不超过9位数。我能知道怎么做吗?代码将在下面插入
<label class="control-label col-sm-5" for="badgeid" style="margin-left: -50px;">ID:</label>;
<div class="col-sm-4">;
<p class="form-control-static" style="margin-top: -6px;">
<input type="number" class="form-control" id="secuid" name="secuid" min="0" placeholder="Enter ID" value=" $return_id">
<label class="control-label col-sm-6" style="color: red; font-size: 12px; margin-right: 20px;display: contents;">** 6 Digit ST Employee ID / 9 SecurityID **</label>
</p>
</div>
<div class="col-sm-10"></div>下面是Javascript。
var secuid = document.translot.secuid.value;
if (secuid == null || secuid == ""){
alert("Please Insert the ID.")
return false;
}发布于 2022-05-10 08:25:57
这是相当简单的-使用‘模式’与最大长度。
Way 1:
<html>
<body>
<h1>The input pattern attribute</h1>
<form action="/action_page.php">
<label for="secuid">Enter ID:</label>
<input type="text" id="secuid" name="secuid" pattern="(\d{6}|\d{9})" maxlength="9" title="Enter ID"><br><br>
<input type="submit">
</form>
</body>
</html>见JSFiddle (1) 这里
Way 2使用minlength=6、maxlength=9,并且只允许数字: pattern="0-9+“。
<input type="text" id="secuid" name="secuid" pattern="[0-9]+" minlength="6" maxlength="9" title="Enter ID">见JSFiddle (2) 这里
注:不需要javascript
发布于 2022-05-10 07:06:20
使用pattern属性和这个regex:
\b\d{6}\b|\b\d{9}\b
/* Start with a non-word character then 6 consecutive digits then
another non-word character OR start with a non-word character then
9 consecutive digits then another non-word character */type="number"忽略pattern属性,信不信由你,所以使用type="tel"
在本例中,通过观察它提供的警告消息来测试它,在尝试发送不符合条件的内容时,它将作为无效的错误消息进行测试。
<form>
<input type='tel' pattern='\b\d{6}\b|\b\d{9}\b' required>
<button>TEST</button>
</form>
https://stackoverflow.com/questions/72181703
复制相似问题