我必须验证文本框,以只输入alpha数字字符。函数validateAlphaNumeric(evt, txtbox)在textbox上触发onkeypress事件。下面是用Javascript编写的函数。
但是如果我做了textbox,我无法得到Ctrl+V的值,我需要验证用户粘贴是否正确。有人能建议我吗?
function validateAlphaNumeric(evt, textBox) {
/* File Description : Numbers,Characters,Hyphen(-),Slash(/)and Space */
var charCode;
charCode = (evt.which) ? evt.which : window.event.keyCode;
if (charCode >= 97 && charCode <= 122 || charCode >= 65 && charCode <= 90 || charCode == 8 || charCode >= 48 && charCode <= 57 || charCode == 45) {
return true;
}
else {
var errorMsg = document.getElementById(textBox.id + 'Error');
if (errorMsg != null) {
errorMsg.innerText = "Please Enter Alpha – Numeric Characters only";
}
return false;
}
}发布于 2014-05-22 11:51:25
我找到了一个答案:在onpaste事件上试试这个。一定会成功的。
我试过这个:
function onPaste(evt, textBox) {
pastedText = window.clipboardData.getData('Text');
if (pastedText matches regExp) {
return true;
} else {
//display error msg
return false;
}
}问候..。
发布于 2014-05-22 05:26:43
验证该的onblur()事件上的文本框。你的问题一定会解决的。
发布于 2014-05-22 05:39:19
从html5上看,有一个oninput事件,它完全可以实现您想要的效果。
https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers.oninput
在处理程序中,检查文本框的value属性仅包含允许的字符。
请注意,IE9对此事件的支持是错误的--基本上它不识别字符删除,但它不会影响您,因为您不能仅通过删除内容就使文本无效。有关更多细节,请参见此处:
http://help.dottoro.com/ljhxklln.php
如果您真的需要支持IE8-IE7而不能使用oninput (提示:您并不真的想这样做),您可以通过监听onpaste事件来修复您的代码,从而获得文本粘贴事件。
https://stackoverflow.com/questions/23798325
复制相似问题