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?
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 SuterCommented:
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

0
RadhaKrishnaKiJayaAuthor Commented:
Thank you for your reply.

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

Thanks.
0
Russ SuterCommented:
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

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

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.
0
Russ SuterCommented:
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/
0

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.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.