<img alt="All TextBoxes except Email type allow only numbers" class="size-full wp-image-261" src="http://taditdash.files.wordpress.com/2014/04/all-textboxes-except-email-type-allow-only-numbers.jpg?w=800" style="height: 463px; width: 500px;" />
All TextBoxes except Email type allow only numbers
Updated on 25 April 2014
Blog Continues...
Inspired by a question, I am blogging about a simple technique to attach KeyPress
Event to all the TextBoxes
of a WebPage.
And What is the Technique?
- Loop through all the Controls in the WebPage
- Find which are
TextBoxes
- For those boxes, attach
KeyPress
Event
We will do it using JavaScript.
Can You Elaborate Each Step?
Why not !!! Let’s explore.
var allInputs = new Array();
allInputs = document.getElementsByTagName('input');
So, allInputs
array would contain all the input elements on the WebPage.
Find Which are TextBoxes
for (i = 0; i < allInputs.length; i++) {
if (allInputs[i].type == 'text') {
}
}
Here, we are looping through all the inputs and checking if its type is text
. If satisfied, it is a TextBox
.
In that if
clause, we can attach any Event we need. We will attach the KeyPress
Event in the next step.
For those Boxes, Attach KeyPress Event
We will attach KeyPress
Event as an example. For that, we will use jQuery KeyPress.
for (i = 0; i < allInputs.length; i++) {
if (allInputs[i].type == 'text') {
$(allInputs[i]).keypress(function (event) {
if (!isNumber(event)) {
event.preventDefault();
}
});
}
}
Here, we attached the KeyPress
Event and inside that Event, we are calling one function isNumber(event)
. If the pressed key is not a number, it would ignore as event.preventDefault()
comes into action, else it will allow.
Note
The function isNumber(event)
just checks if the pressed key is a numeric
key or not. Refer to the demo link given below to see the code for this function.
See This In Action
See demo here.
Update, Improvements and Quicker jQuery Approach
Thanks to Kshirodra Meher and Subhajit Datta for the valuable suggestions.
What They Suggested?
In our first step, we looped through all the TextBoxes
present in the Page, which can be optimized with a simpler jQuery block coded below.
$("input[type=text]").bind("keypress", function (e) {
if (!isNumber(e)) {
return false;
}
});
So, the main game changer here is the Selector $("input[type=text]")
. It selects all the inputs of type text
. To know more, click Attribute Equals Selector [name="value"]. We have also restricted the paste event on the TextBoxes
because it was allowing all characters into the box.
$("input[type=text]").bind("paste", function (e) {
return false;
});
New Demo
Check out the new demo here.
Share Your Thoughts !!!
If you like the blog, share among your friends. Feel free to comment if you have any doubts or queries.
CodeProject
<img border="0" src="http://feeds.wordpress.com/1.0/comments/taditdash.wordpress.com/250/" /> <img border="0" height="1" src="http://stats.wordpress.com/b.gif?host=taditdash.wordpress.com&blog=60876978&post=250&subd=taditdash&ref=&feed=1" width="1" />