本文实例讲述了JavaScript监听文本框回车事件并过滤文本框空格的方法。分享给大家供大家参考。具体如下:
获取文本框
onload = function()
{
username = document.getElementById("txtUserName");
password = document.getElementById("txtPassWord");
}
//清空文本框
function clearTxt()
{
username.value = "";
password.value = "";
username.focus();
// document.getElementById('txtUserName').value="";
// document.getElementById('txtPassWord').value="";
// document.getElementById('txtUserName').focus();
}
//确定
function chkTxt()
{
//删除前后空格
username.value = username.value.replace(/(^\s*)|(\s*$)/g,"");
password.value = password.value.replace(/(^\s*)|(\s*$)/g,"");
//判空
if(username.value.length == 0)
{
alert("请输入用户名!");
username.focus();
}
else if(password.value.length == 0)
{
alert("请输入密码!");
password.focus();
}
else
document.getElementById("btnLogin").click();
}
//回车监听
function onkey()
{
if (window.event.keyCode==13)
{
// document.all["btnLogin"].focus();
// if(document.activeElement.id = "aReset")
//判断当前焦点所在的控件的id是aReset
// {
// document.getElementById("aReset").focus();
// }
document.getElementById("aLogin").focus();
return false;
}
}
css代码:
密码:
<%----%>