【发布时间】:2013-02-25 12:52:40
【问题描述】:
我有这个 JS 函数可以防止用户输入字符
<script type="text/javascript">
function validate(evt) {
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
key = String.fromCharCode(key);
var regex = /[0-9]|\./;
if(!regex.test(key)) {
theEvent.returnValue = false;
if(theEvent.preventDefault) theEvent.preventDefault();
}
}
</script>
<span>Radio Receive:</span>
<input name="ReceiveNo" type="text" class="txtbox" onkeypress='validate(event)' maxlength="11" value="${cpCon.receiveNo}" required tabindex="34" />
但我注意到当用户试图从这个文本框中粘贴一个单词时,可以输入该文本。如何在不禁用粘贴的情况下防止这种情况发生?
【问题讨论】:
-
<input readonly>或inputElement.readonly = true? -
查看onkeydown 事件以阻止控制字符。
-
检查这个类似的问题.. stackoverflow.com/questions/5510129/…的可能重复
标签: javascript html validation