我需要验证一个文本框,它应该只允许某些字符,并且在达到限制后,文本框不应允许在其中输入字符。它可以通过 Javascript 完成,但不知道该怎么做。任何人请帮助。 .
最佳答案
这里我提供了两种解决方案: 1)js函数'TextLimit':它会检查输入到文本框中的字符数。 2)maxlength属性:指定输入字段的最大长度(以字符为单位)。
<HTML>
<HEAD>
<TITLE>Test</TITLE>
<script>
function TextLimit(){
var text = document.getElementById('myinput');
if (text.value.length > 10){
alert('You exceeded the limit');
text.value = text.value.substring(0,10);
}
}
</script>
</HEAD>
<BODY>
<input name="myinput" type="text" value='' id="myinput" onkeyup="TextLimit()">
<br>
//this is using the maxlength attribute of textbox
<input type="text" name="inputtext" maxlength="10">
</BODY>
</HTML>
关于javascript - 使用Javascript限制在文本框中输入的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3359518/