限制ASP某文本框只能输入6位数字

来源:百度知道 编辑:UC知道 时间:2024/05/18 02:43:46
(不能多于6位也不能少于6位),且需要在提交表单前(失去焦点后)即给出提示信息,即在光标离开该文本框之后(不用提交表单也不用加判断按钮),马上在文本框边上给出提示?

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<script language="javascript">
function OnlyNumber(){
if ( !((window.event.keyCode >= 48)&&(window.event.keyCode <= 57))){
window.event.keyCode = 0 ;
}
}
function testLen(obj){
if (obj.value.length!=6){
document.getElementById("msg").innerHTML="长度必须为6";
obj.focus();
}
}
</script>
<body>
<form name="form1">
<input type="text" maxlength="6" style='ime-mode:disabled' onKeyPress="OnlyNumber()" onBlur="testLen(this)"/><span id="msg" style="color:red;"></span><br>
其中,文本框的样式:style="ime-mode:disabled"用于禁止切换输入法,不可去掉。
</form