Skip to main content

Masked Textbox in asp.net

Whenever we need the textbox control for date, phone numbers with area code with seperated, in these cases the masked textbox will be helpful. It takes delimiter after specific word automatically.
Below is simple example to demonstrate the phone number with STD, Area code.

HTML form

<form name="form" action="" method="post">

<input name="Field1" value="" type="text" 
onKeyPress="javascript:return maskInput(this.value,this,'3,7','-');"
onblur="javascript:return maskInput(this.value,this,'3,7','-');"
"
style="font-family:verdana;font-size:10pt;width:110px;"
maxlength="12">

</form>

The Function which we use for masking

//Masked the input phone number to display in "123-123-1234" format
function maskInput(input, textbox, location, delimiter) {

    //Get the delimiter positons
    var locs = location.split(',');

    //Iterate until all the delimiters are placed in the textbox
    for (var delimCount = 0; delimCount <= locs.length; delimCount++) {
        for (var inputCharCount = 0; inputCharCount <= input.length; inputCharCount++) {

            //Check for the actual position of the delimiter
            if (inputCharCount == locs[delimCount]) {

                //Confirm that the delimiter is not already present in that position
                if (input.substring(inputCharCount, inputCharCount + 1) != delimiter) {
                    input = input.substring(0, inputCharCount) + delimiter + input.substring(inputCharCount, input.length);
                }
            }
        }
    }
    textbox.value = input;
}

Popular posts from this blog

Asp.Net Web API

What is ASP.NET Web API? ASP.NET Web API is a framework provided by the Microsoft with which we can easily build HTTP services that can reach a broad of clients, including browsers, mobile, IoT devices, etc. ASP.NET Web API provides an ideal platform for building RESTful applications on the .NET Framework.   Difference between ASP.NET Web API and WCF Web AP I is a Framework to build HTTP Services that can reach a board of clients, including browsers, mobile, IoT Devices, etc. and provided an ideal platform for building RESTful applications. It is limited to HTTP based services. ASP.NET framework ships out with the .NET framework and is Open Source. WCF i.e. Windows Communication Foundation is a framework used for building Service Oriented applications (SOA) and supports multiple transport protocol like HTTP, TCP, MSMQ, etc. It supports multiple protocols like HTTP, TCP, Named Pipes, MSMQ, etc. WCF ships out with the .NET Framework. Both Web API and WCF can be self-hosted or can be...

Creating package in Oracle Database using Toad For Oracle

What are Packages in Oracle Database A package is  a group   of procedures, functions,  variables   and  SQL statements   created as a single unit. It is used to store together related objects. A package has two parts, Package  Specification  and Package Body.