Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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.
Posted
Updated 19-Feb-15 3:17am
v4
Comments
Sergey Alexandrovich Kryukov 16-Feb-15 10:41am    
It's "onkeyup"...
—SA
Андрей Голубцов 19-Feb-15 9:20am    
Your comment with my grammatic mistake "really" help me.
Why do you post this comment ? For reputation?
Sergey Alexandrovich Kryukov 19-Feb-15 10:43am    
Reputation?! Please relax and don't be so ridiculous. Are you going to get irritated in each comment? I can see you fixed the spelling, so it was some help. What's wrong with that? I would gladly help by at that time saw you already got enough help.
—SA

1 solution

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

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.
 
Share this answer
 
Comments
Андрей Голубцов 16-Feb-15 10:11am    
@ProgramFOX Thanks ! But I have an error :"Runtime Error JavaScript: Failed to set property "onkeyup" reference value is not specified or is NULL"
Thomas Daniels 16-Feb-15 10:27am    
That's related to the location of your script. Either put my code in a script-tag at the end of your body-tag, or wrap it in a window.onload event handler.
Андрей Голубцов 17-Feb-15 2:29am    
I write it in page_load
dr.Attributes.Add("onkeyup", "myFun('"+dr+"');");
and my script to the head :
<script type="text/javascript">
function myFun(obj) {
if (/^[0-9]{2}(.[0-9]{2})?$/.test(obj.value)) {
this.value += ".";
}
}
</script>

I dont have error but it still doesn't work . If you have any idea how it fix I will be grateful
Thomas Daniels 17-Feb-15 3:28am    
Try this: at the end of your <body> tag, add this script:

<script type="text/javascript>
document.getElementById("<%= yourTextbox.ClientID %>").onkeyup = function() {
if (/^[0-9]{2}(.[0-9]{2})?$/.test(this.value)) {
this.value += ".";
}
}
</script>
Андрей Голубцов 17-Feb-15 3:38am    
i don't have <body> tag . I make this is depend asp.net page . I have only two ContentPlace holder tags(head and Content1) and all my construcrion are in them .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900