只允许在文本框中预定义charecter [英] allow only predefine charecter in textbox
本文介绍了只允许在文本框中预定义charecter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好......
我有一个html文本框。我想通过javascript只允许文本框中的预定义字符。
Hello...
I have a html textbox. I want to allow only predefine charecter in textbox by javascript.
<input type="text" id="txt1" onkeypress="allowChar(this, 'ABC123');" />
这是一个文本框示例。其中只允许资本A,资本B,资本C和数字仅1,2,3。
Here an example of textbox. In which allow only capital A, capital B, capital C and in numeric only 1,2,3.
<script type="text/javascript>
function allowChar(txt, char)
{
........
........
........
}
</script>
请按照您的意愿通过javascript或jquery解决这个问题。
谢谢
问候
Rakesh
Please solve this problem by javascript or jquery as you wish.
Thanks
Regards
Rakesh
推荐答案
您已经将Textbox对象和您的预定义字符传递给您的函数将那些字符与密钥代码匹配[ ^ ]试试这个:
HTML:
You are already passing Textbox Object and Your Predefined Chars to your function. Match the chars there with key codes[^]. Try this:
HTML:
<input type="text" id="txt1" class="customChar" önkeypress="return allowChar(this, 'ABC123');" />
JavaScript:
JavaScript:
function allowChar(textbox, chars){
//check with key code here
var keynum;
if(window.event){ // IE
keynum = e.keyCode;
}else
if(e.which){ // Netscape/Firefox/Opera
keynum = e.which;
}
var EnteredWord = String.fromCharCode(keynum); //Here you got your pressed key
//If matching with the predefined characters stored in 'chars' then return true otherwise return false.
}
希望它有所帮助!
--Amit
Hope it helps!
--Amit
请参阅链接。它可能对你有所帮助。
防止对html文本字段输入特殊字符
event.preventDefault()
Refer to the links. It may help you a bit.
Preventing special character input to html text field
event.preventDefault()
谷歌是你的朋友:好好经常拜访他。他可以比在这里发布问题更快地回答问题...
非常快速的搜索给出了200,000次点击:\"javascript限制文本框 [ ^ ]
将来,请尝试自己做至少基础研究,不要浪费你的时间或我们的时间。
Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...
A very quick search gave 200,000 hits: "javascript restrict textbox"[^]
In future, please try to do at least basic research yourself, and not waste your time or ours.
这篇关于只允许在文本框中预定义charecter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文