日期:2014-05-16 浏览次数:20482 次
<!DOCTYPE> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script type="text/javascript"> function checkSpar(text){ if(/@/.test(text.value)){ alert("不能包含@字符"); text.value = text.value.replace("@",""); } } </script> </head> <body> <input type="text" onKeyup="checkSpar(this);"> </body> </html>
------解决方案--------------------
onkey类事件(DOWN,UP,PRESS)都可以得到当前按下的键值,与@的键值比较.
获得文本框中光标位置的通用代码:
<script>
function getTxt1CursorPosition(){
var oTxt1 = document.getElementById("txt1");
var cursurPosition=-1;
if(oTxt1.selectionStart){//非IE浏览器
cursurPosition= oTxt1.selectionStart;
}else{//IE
var range = document.selection.createRange();
range.moveStart("character",-oTxt1.value.length);
cursurPosition=range.text.length;
}
alert(cursurPosition);
}
</script>
<input type="text" id="txt1" value="abcde" onclick="getTxt1CursorPosition()">