只能是数字、字母、-和_
在文本框的keypress事件調用下面函數。
如 <input disabled="disabled" type="text" id='userNameToEdit' οnkeypress="TextValidate()" />
如果在文本框中按下特殊字符鍵,則顯示警告信息,或者輸入框不接受非法輸入。
function TextValidate()
{
??? var code;
??? var character;
??? var err_msg = "Text can not contain SPACES or any of these special characters? other than underscore (_) and hyphen (-).";
??? if (document.all) //判斷是否是IE瀏覽器
??? {
??????? code = window.event.keyCode;
??? }
??? else
??? {
??????? code = arguments.callee.caller.arguments[0].which;
??? }
??? var character = String.fromCharCode(code);
????
??? var txt=new RegExp("[ ,\\`,\\~,\\!,\\@,\#,\\$,\\%,\\^,\\+,\\*,\\&,\\\\,\\/,\\?,\\|,\\:,\\.,\\<,\\>,\\{,\\},\\(,\\),\\',\\;,\\=,\"]");
??? //特殊字符正則表達式(放到[]里,并用\\轉義)
??? if (txt.test(character))
??? {
??????? alert("用戶名不能包括空格或以下字符:\n? , ` ~ ! @ # $ % ^ + & * \\ / ? | : . < > {} () [] \" "); //去掉此句,當輸入非法信息是沒有任何反應
??????? if (document.all)
??????? {
??????????? window.event.returnValue = false;
??????? }
??????? else
??????? {
??????????? arguments.callee.caller.arguments[0].preventDefault();
??????? }
??? }
}
轉載于:https://www.cnblogs.com/yinluhui0229/archive/2011/04/23/2025469.html
總結
以上是生活随笔為你收集整理的只能是数字、字母、-和_的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 添加firefox4的deb源,直接安装
- 下一篇: border属性的CSS缩写顺序