限制只能输入数字程序的错误

kairwen 2003-08-20 10:19:07
function checkint(me){
var s="0123456789"
var c=String.fromCharCode(event.keyCode);
if (s.indexOf(c)<0){
event.keyCode=0;
return false;
}
}
上述javascript函数用来限制只能输入数字(onKeyPress),但是中文还是显示出来了,这是为什么?
...全文
90 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
biggie 2003-08-20
  • 打赏
  • 举报
回复
必须输入数字
function checkInteger(objInteger){
if (!checkNull(objInteger))return false;
if (isNaN(document.all(objInteger).value)) return false;
var Reg=/^\d*\.\d*$/;
if (Reg.test(document.all(objInteger).value)) return false;
return true;
}
hxzhappy 2003-08-20
  • 打赏
  • 举报
回复
天下一大抄
hfprogramer 2003-08-20
  • 打赏
  • 举报
回复
呵呵,抄袭之做,也许有帮助。

-----------------------------------------------------------------

<HTML>
<HEAD>
<TITLE>Key Input Test</TITLE>
<script language="JavaScript">
var keybYN = new keybEdit('yn','Valid values are \'Y\' or \'N\'.');
var keybNumeric = new keybEdit('01234567890','Numeric input only.');
var keybAlpha = new keybEdit('abcdefghijklmnopqurstuvwxy ','Alpha input only.');
var keybAlphaNumeric = new keybEdit('abcdefghijklmnopqurstuvwxy01234567890 ','Alpha-numeric input only.');
var keybDecimal = new keybEdit('01234567890.','Decimal input only.');
var keybDate = new keybEdit('01234567890/','Date input only');;
var keybYNNM = new keybEdit('yn');
var keybNumericNM = new keybEdit('01234567890');
var keybAlphaNM = new keybEdit('abcdefghijklmnopqurstuvwxy');
var keybAlphaNumericNM = new keybEdit('abcdefghijklmnopqurstuvwxy01234567890');
var keybDecimalNM = new keybEdit('01234567890.');
var keybDateNM = new keybEdit('01234567890/');;

function keybEdit(strValid, strMsg) {
/* Function: keybEdit
Creation Date: October 11, 2001
Programmer: Edmond Woychowsky
Purpose: The purpose of this function is to be a constructor for
the keybEdit object. keybEdit objects are used by the
function editKeyBoard to determine which keystrokes are
valid for form objects. In addition, if an error occurs,
they provide the error message.

Please note that the strValid is converted to both
upper and lower case by this constructor. Also, that
the error message is prefixed with 'Error:'.

The properties for this object are the following:
valid = Valid input characters
message = Error message

The methods for this object are the following:
getValid() = Returns a string containing valid
characters.
getMessage()= Returns a string containing the
error message.

Update Date: Programmer: Description:
*/

// Variables
var reWork = new RegExp('[a-z]','gi'); // Regular expression\

// Properties
if(reWork.test(strValid))
this.valid = strValid.toLowerCase() + strValid.toUpperCase();
else
this.valid = strValid;

if((strMsg == null) || (typeof(strMsg) == 'undefined'))
this.message = '';
else
this.message = strMsg;

// Methods
this.getValid = keybEditGetValid;
this.getMessage = keybEditGetMessage;

function keybEditGetValid() {
/* Function: keybEdit
Creation Date: October 11, 2001
Programmer: Edmond Woychowsky
Purpose: The purpose of this function act as the getValid method
for the keybEdit object. Please note that most of the
following logic is for handling numeric keypad input.

Update Date: Programmer: Description:
*/

return this.valid.toString();
}

function keybEditGetMessage() {
/* Function: keybEdit
Creation Date: October 11, 2001
Programmer: Edmond Woychowsky
Purpose: The purpose of this function act as the getMessage method
for the keybEdit object.

Update Date: Programmer: Description:
*/

return this.message;
}
}




void function editKeyBoard(objForm, objKeyb) {
/* Function: editKeyBoard
Creation Date: October 11, 2001
Programmer: Edmond Woychowsky
Purpose: The purpose of this function is to edit edit keyboard input
to determine if the keystrokes are valid.

Update Date: Programmer: Description:
*/

strWork = objKeyb.getValid();
strMsg = ''; // Error message
blnValidChar = false; // Valid character flag

// Part 1: Validate input
if(!blnValidChar)
for(i=0;i < strWork.length;i++)
if(window.event.keyCode == strWork.charCodeAt(i)) {
blnValidChar = true;

break;
}

// Part 2: Build error message
if(!blnValidChar) {
if(objKeyb.getMessage().toString().length != 0)
alert('Error: ' + objKeyb.getMessage());

window.event.returnValue = false; // Clear invalid character
objForm.focus(); // Set focus
}
}

void function setEvents() {
document.all.txtYN.onkeypress = new Function('editKeyBoard(this,keybYN)');
document.all.txtNumeric.onkeypress = new Function('editKeyBoard(this,keybNumeric)');
document.all.txtAlpha.onkeypress = new Function('editKeyBoard(this,keybAlpha)');
document.all.txtAlphaNumeric.onkeypress = new Function('editKeyBoard(this,keybAlphaNumeric)');
document.all.txtDecimal.onkeypress = new Function('editKeyBoard(this,keybDecimal)');
document.all.txtDate.onkeypress = new Function('editKeyBoard(this,keybDate)');
}
</script>
</HEAD>
<body onLoad="JavaScript:setEvents()">
<TABLE WIDTH=100% BORDER=0 CELLSPACING=1 CELLPADDING=1>
<TR>
<TD align="right">YN: </TD>
<TD align="left"><INPUT type="text" id=txtYN name=txtYN></TD>
</TR>
<TR>
<TD align="right">Numeric: </TD>
<TD align="left"><INPUT type="text" id=txtNumeric name=txtNumeric></TD>
</TR>
<TR>
<TD align="right">Alpha: </TD>
<TD align="left"><INPUT type="text" id=txtAlpha name=txtAlpha></TD>
</TR>
<TR>
<TD align="right">AlphaNumeric: </TD>
<TD align="left"><INPUT type="text" id=txtAlphaNumeric name=txtAlphaNumeric></TD>
</TR>
<TR>
<TD align="right">Decimal: </TD>
<TD align="left"><INPUT type="text" id=txtDecimal name=txtDecimal></TD>
</TR>
<TR>
<TD align="right">Date: </TD>
<TD align="left"><INPUT type="text" id=txtDate name=txtDate></TD>
</TR>
</TABLE>
</body>
</HTML>

----------------------------------------------------------------------
zlhlj2000 2003-08-20
  • 打赏
  • 举报
回复
正则表达式
if(yourString.search(/^[0-9/]*$/g) ==-1){
alert("must input number");
return false;
}

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧