Click here to Skip to main content
15,881,882 members
Articles / Web Development / ASP.NET

Attach Event to all the TextBoxes in a WebPage

24 Apr 2014CPOL2 min read 10.7K   4  
Inspired by a question, I am blogging about a simple technique to attach KeyPress Event to all the TextBoxes of a WebPage.

 

All TextBoxes except Email type allow only numbers

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.

Loop Through All the Controls in the WebPage

JavaScript
var allInputs = new Array();

// store collection of all <input/> elements
allInputs = document.getElementsByTagName('input');

So, allInputs array would contain all the input elements on the WebPage.

Find Which are TextBoxes

JavaScript
for (i = 0; i < allInputs.length; i++) {
    // loop through and find <input type="text"/>
    if (allInputs[i].type == 'text') {
        // This is a TextBox. Here we can attach any Event.
    }
}

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.

JavaScript
for (i = 0; i < allInputs.length; i++) {
    // loop through and find <input type="text"/>
    if (allInputs[i].type == 'text') {
        // This is a TextBox. Here we can attach any Event.
        // Let's attach KeyPress Event to the TextBox.
        $(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.

JavaScript
$("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.

JavaScript
$("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.

License

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


Proud Indian | Author | TEDx Speaker | Microsoft MVP | CodeProject MVP | Speaker | DZone Most Valuable Blogger| jsfiddler

My Website

taditdash.com

Programming Community Profiles

jsfiddle | Stack Overflow

Social Profiles

Facebook | Twitter | LinkedIn

Awards


  1. DZone Most Valuable Blogger
  2. Microsoft MVP 2014, 2015, 2016, 2017, 2018
  3. Code Project MVP 2014, 2015, 2016
  4. Star Achiever of the Month December 2013
  5. Mindfire Techno Idea Contest 2013 Winner
  6. Star of the Month July 2013

Comments and Discussions

 
-- There are no messages in this forum --