Masked edit textbox control in ASP.NET

Hi Experts,

I have a textbox to enter phone number in 111-111-1111 format. I do not want to type dash (-) all the time. I want the cursor to go to the next field when I type 111. I am using ASP.NET and VB.NET.

Thanks.
RadhaKrishnaKiJayaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russ SuterSenior Software DeveloperCommented:
you should be able to do something like this using jQuery. Example:

$('#fieldName').keyup(function (e) {
            if (e.keyCode != 16) {
                if (e.keyCode != 9) {
                    if ($.trim($('#fieldName').val()).length == "5") {
                        $('#nextField').focus();
                    }
                }
            }
        });

Open in new window

RadhaKrishnaKiJayaAuthor Commented:
Thank you for your reply.

I could not understand how the above code will be helpful to me.

Thanks.
Russ SuterSenior Software DeveloperCommented:
Here's a complete example. Paste this into an html page and it'll work.

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $('.phonepart').keyup(function (e) {
            if (e.keyCode != 16) {
                if (e.keyCode != 9) {
                    if ($.trim($(e.target).val()).length === e.target.maxLength) {
                        e.preventDefault();
                        var inputs = $(this).closest('form').find(':input:visible');
                        inputs.eq( inputs.index(this)+ 1 ).focus();
                    }
                }
            }
        });
    });
</script>
</head>
<body>
  <form id="phonenumber">
    <input class="phonepart" id="areacode" style="width: 40px;" maxlength="3"/> -
    <input class="phonepart" id="phoneprefix" style="width: 40px;" maxlength="3"/> -
    <input class="phonepart" id="phonesuffix" style="width: 60px;" maxlength="4"/>
  </form>
</body>
</html>

Open in new window

Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

RadhaKrishnaKiJayaAuthor Commented:
Thank you very much for the code. But I do not want three different text boxes. I want the same operation in one text box with two dashes. If you try to create an yahoo A/C, in the registration form they have a field to enter the phone number. Where the brackets appear automatically when you start entering the numbers. If you can help me to do something like that then it will be really great.
Russ SuterSenior Software DeveloperCommented:
Your original post said you wanted the cursor to go to the next field after typing 111. That's exactly what this does. If you want the entire phone number in a single field then I'd suggest using a jQuery plugin. This one is very easy to use:

http://digitalbush.com/projects/masked-input-plugin/

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RadhaKrishnaKiJayaAuthor Commented:
Thank you very much for your help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.