SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
Thread: How to create an event listener?
Hybrid View
-
Jan 5, 2007, 07:48 #1
- Join Date
- Dec 2006
- Posts
- 7
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
How to create an event listener?
Hello,
How do I add an event listener to a few text boxes that prevents the user from typing anything but digits. I use the numbers entered in the text boxes in calculations afterwards so I don't want the user to enter "one" instead of "1" etc... Also, the range of possible numbers is too big for a drop down menu.
Please help :-)
Thanks!
Julie
-
Jan 5, 2007, 08:00 #2
- Join Date
- May 2003
- Location
- Cambridge, UK
- Posts
- 2,366
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Code:my_input.onkeyup = function () { this.value = this.value.replace(/\D/g,''); }
-
Jan 10, 2007, 14:36 #3
- Join Date
- Dec 2006
- Posts
- 7
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks to both of you for your suggestions. I will try both... First of all, for this one:
I assume I place the above code in my js file. Now what do I need to add in my text input line to call the above code in my js file?:
Code:<input name="txtTest" type="text" id="txtTest" size="4" maxlength="4" tabindex="1">
Thanks for your help!
Julie
-
Jan 10, 2007, 16:19 #4
- Join Date
- May 2003
- Location
- Cambridge, UK
- Posts
- 2,366
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Code:<input name="txtTest" type="text" id="txtTest" size="4" maxlength="4" tabindex="1"> <script type="text/javascript"> document.getElementById('txtTest').onkeyup = function () { this.value = this.value.replace(/\D/g,''); } </script>
-
Jan 11, 2007, 08:55 #5
- Join Date
- Dec 2006
- Posts
- 7
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
It works! Thanks a lot!
Have a great day!
Julie
-
Jan 5, 2007, 08:04 #6
- Join Date
- Jul 2006
- Posts
- 151
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Event Listener
What I would do is use the 'onblur' event of the textbox, and then validate for a numeric response there.
So for instance:
HTML Code:<input type="text" size="10" onblur="checkNumeric(this)" />
Code:function checkNumeric(el) { if (!el.value) { alert("You must enter a value"); el.focus(); return false; } if (parseInt(el.value)==el.value) return true; alert("You must enter a numeric value"); el.focus(); return false; }
-
Jan 10, 2007, 14:46 #7
- Join Date
- Jul 2006
- Posts
- 151
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Looks like to me that you won't need to add anything, just change my_input to txtTest.
Dave
Bookmarks