我该怎么做onkeyup日期事件 [英] How can I do onkeyup date event

查看:97
本文介绍了我该怎么做onkeyup日期事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以建议我如何在文本框中制作onkeyup事件,以便将文本转换为日期吗?

示例:我写的是11022013,我将有11.02.2013。但我需要它动态 - 在我将在文本框中写11后,我会看到11。在11.02之后,我会看到11.02。在11.02.2013​​之后,我将看到11.02.2013​​。

我希望能够回答或链接到那个很好的解释。

P.S. Asp.net c#framework 2.0 javascript。

Can someone suggest how I can make onkeyup event in textbox for converting text to date ?
Example: I write 11022013 and i will have 11.02.2013 . But i need it dynamic - after I will write "11" in the textbox i will see "11." , after "11.02" i will see "11.02." and after "11.02.2013" i will see "11.02.2013" .
I hope for answer or links to good explain of that.
P.S. Asp.net c# framework 2.0 javascript.

推荐答案

试试这个:

Try this:
document.getElementById("<%= yourTextbox.ClientID %>").onkeyup = function() {
    if (/^[0-9]{2}(.[0-9]{2})?


/ .test( this value )){
this value + = ;
}
}
/.test(this.value)) { this.value += "."; } }



这是做什么用的?在 keyup 上,它根据正则表达式测试文本框的值,该正则表达式检查文本框是由2位数字组成还是2位数字+点数+ 2位数字。如果是,则添加另一个点。



重要说明:如果要在服务器上使用文本框值,那么即使使用上面的代码,也应该在服务器端验证它。即使onkeyup函数存在,也不会阻止人们在文本框中放置他们想要的东西,因此你应该总是在服务器端验证。


What does this do? On keyup, it tests the value of the textbox against a regex that checks whether the textbox consists of 2 digits or of 2 digits + a dot + 2 digits. If it does, then it adds another dot.

Important note: if you want to use the textbox value on the server, then even with the above code, you should validate it at server-side. Even while the onkeyup function is there, that doesn't stop people from putting whatever they want in the textbox, hence you should always validate on server-side.


这篇关于我该怎么做onkeyup日期事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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