| // 自行处理按钮 switch (nKeyCode) { case 8:// 如果动作是退格[<-] { strText = strText.substr(0,nCursorPos-1) + strText.substr(nCursorPos, nTextLen-nCursorPos); nCursorPos--; break; } case 46:// 如果动作是del[del] { strText = strText.substr(0,nCursorPos) + strText.substr(nCursorPos+1,nTextLen-nCursorPos-1); nCursorPos--; break; } case 38:// 如果动作是方向键[上] case 39:// 如果动作是方向键[右] { nCursorPos++; break; } case 37:// 如果动作是方向键[左] case 40:// 如果动作是方向键[下] { nCursorPos--; break; } default : { strText = strText.substr(0,nCursorPos) + String.fromCharCode(nKeyCode) + strText.substr(nCursorPos,nTextLen); nCursorPos++; } if (strText.match(expMask)) // 移动光标 // 得到一个文本框控件的当前光标位置 |
| obj.focus(); var workRange=document.selection.createRange(); obj.select(); var allRange=document.selection.createRange(); workRange.setEndPoint("StartToStart",allRange); var len=workRange.text.length; workRange.collapse(false); workRange.select(); return len; // 设置一个文本框控件的当前光标位置 range.select(); |
使用方法:
1.设置默认的、不定的格式初值。如:日期时间的格式初值为“// ::”,表示(年/月/日 时:分:秒)。IP的为“...”(192.168.0.1)。其实就是设置一个不会违反正则表达式的字符就可以。
2.在表单的TEXT框的OnKeyDown事件中调用mask函数,参数objTextBox是指定的文本框的name。参数mask是正则表达式格式的掩码。
例子:
要使用日期时间型的掩码框
| <input name="i_etmend" type="text" id="i_etmend" value="{I_ETMEND}" maxlength="19" onkeydown="mask(i_etmend, '^(([0-9]{0,4})\-([0-9]{0,2})\-([0-9]{0,2}) ([0-9]{0,2}):([0-9]{0,2}):([0-9]{0,2}))$')"> |
要使用IP掩码框
| <input name="i_bip" type="text" id="i_bip" value="{I_BIP}" maxlength="15" onkeydown="mask(i_bip, '^([0-9]{0,3}[\.][0-9]{0,3}[\.][0-9]{0,3}[\.][0-9]{0,3})$')"> |
[首页] [上一页] [下一页] [末页]
