使用javascript限制用户输入 [英] restrict user input using javascript

查看:74
本文介绍了使用javascript限制用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能告诉我一些限制Javascript中用户输入(在输入标签上)的例子吗?

Can someone show me some example for restricting user input (on input tag) in Javascript?

我们设置输入时的东西(type =text)只接受数字,所以它会忽略除数字之外的任何其他输入...

Something that when we set the input (type="text") to only accept numeric, so it will ignore any other input except for numeric...

我认为它对数字输入很方便(比如拉链,信用卡,钱,价值,得分,日期等...),如果你能告诉我如何用模式创建输入,例如:

I think it's handy for number input (such as zip, credit card, money, value, score, date etc...), and if you can please show me how to create input with pattern, something like:


Please Input Date:
|-----------------|
|     /     /     |
|-----------------|

PS:
我听说WebForms 2.0将来会支持这个...(符合Acid 3标准的浏览器?)

PS: I heard WebForms 2.0 will support this in the future... (Acid 3 compliant browser?)

输入类型=日期


输入类型=时间

输入类型=数字

输入类型=钱

input type="date"
input type="time"
input type="number"
input type="money"

但它只是来自未来的新闻:D

But it was only news from future :D

推荐答案

这可能对你有帮助。

http://www.w3schools.com/jsref/event_onkeydown.asp

<html>
<body>

<script type="text/javascript">
function noNumbers(e)
{
var keynum;
var keychar;
var numcheck;

if(window.event) // IE
{
keynum = e.keyCode;
}
else if(e.which) // Netscape/Firefox/Opera
{
keynum = e.which;
}
keychar = String.fromCharCode(keynum);
numcheck = /\d/;
return !numcheck.test(keychar);
}
</script>

<form>
<input type="text" onkeydown="return noNumbers(event)" />
</form>

</body>
</html>

这篇关于使用javascript限制用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆