一段简单的javascript代码,可以禁止向文本框中输入某些字符.
<html>
<head>
<title>Untitled Page</title>
<script type="text/javascript">
var Avisnet = {
maskEdit : function(pattern) {
var src = event.srcElement;
var selRange = document.selection.createRange();
var srcRange = src.createTextRange();
selRange.setEndPoint("StartToStart", srcRange);
var num = selRange.text + String.fromCharCode(event.keyCode) + srcRange.text.substr(selRange.text.length);
event.returnValue = pattern.test(num);
}
};
</script>
</head>
<body>
<input type="text" name="username" οnkeypress="Avisnet.maskEdit(/^[\w]*$/)" />
</body>
</html>
示例:在下面的文本框中只能输入字母,数字和下划线
转载于:https://www.cnblogs.com/avisnet/archive/2006/09/09/499453.html