Solved

A-Z 0-9 dash space or apostrophe in javascript

Posted on 2011-09-26
7
539 Views
Last Modified: 2012-05-12
Experts,

In my code shown below I need to only allow A-Z, 0-9, a dash, a space or an apostrophe.  Any ideas?
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#<%= Let1.ClientID %>').keyup(function (event) {
                var c = String.fromCharCode(event.keyCode);
                var isWordcharacter = c.match(/\w/);

                if (isWordcharacter) {
                    $('#<%= Let2.ClientID %>').focus();
                    $('#<%= Let2.ClientID %>').select();
                }

            });
            $('#<%= Let2.ClientID %>').keyup(function (event) {
                var c = String.fromCharCode(event.keyCode);
                var isWordcharacter = c.match(/\w/);

                if (isWordcharacter) {
                    $('#<%= Let3.ClientID %>').focus();
                    $('#<%= Let3.ClientID %>').select();
                }
            });
            $('#<%= Let3.ClientID %>').keyup(function (event) {
                var c = String.fromCharCode(event.keyCode);
                var isWordcharacter = c.match(/\w/);

                if (isWordcharacter) {
                    $('#<%= Let4.ClientID %>').focus();
                    $('#<%= Let4.ClientID %>').select();
                }
            });
            $('#<%= Let4.ClientID %>').keyup(function (event) {
                var c = String.fromCharCode(event.keyCode);
                var isWordcharacter = c.match(/\w/);

                if (isWordcharacter) {
                    $('#<%= Let5.ClientID %>').focus();
                    $('#<%= Let5.ClientID %>').select();
                }
            });
            $('#<%= Let5.ClientID %>').keyup(function (event) {
                var c = String.fromCharCode(event.keyCode);
                var isWordcharacter = c.match(/\w/);

                if (isWordcharacter) {
                    $('#<%= Let6.ClientID %>').focus();
                    $('#<%= Let6.ClientID %>').select();
                }
            });
            $('#<%= Let6.ClientID %>').keyup(function (event) {
                var c = String.fromCharCode(event.keyCode);
                var isWordcharacter = c.match(/\w/);

                if (isWordcharacter) {
                    $('#<%= Let7.ClientID %>').focus();
                    $('#<%= Let7.ClientID %>').select();
                }
            });
            $('#<%= Let7.ClientID %>').keyup(function (event) {
                var c = String.fromCharCode(event.keyCode);
                var isWordcharacter = c.match(/\w/);

                if (isWordcharacter) {
                    $('#<%= btnView.ClientID %>').focus();
                }
            });
        });
    </script>

Open in new window

0
Comment
Question by:us1975mc
  • 3
  • 3
7 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 36601212
var isWordcharacter = /[a-z\d\-\s']/i.test( c );
0
 
LVL 12

Expert Comment

by:jagssidurala
ID: 36601240
0
 

Author Comment

by:us1975mc
ID: 36601606
leakim971,

I replaced my line of code -var isWordcharacter = c.match(/\w/); - with your line of code - var isWordcharacter = /[a-z\d\-\s']/i.test( c );  - and I can still type anything in the textbox.

jagssidurala,

The MaskedEditValidator is interfering with the javascript and I can not run the validator.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36601663
replace all your code by :

$('#<%= Let1.ClientID %>,#<%= Let2.ClientID %>,#<%= Let3.ClientID %>,#<%= Let4.ClientID %>,#<%= Let5.ClientID %>,#<%= Let6.ClientID %>,#<%= Let7.ClientID %>').keyup(function() {
    $(this).val( $(this).val().replace( /[^a-z\d\-\s']/gi, "") );
});

test page : http://jsfiddle.net/Qkag8/
0
 

Author Comment

by:us1975mc
ID: 36601979
leakim971,

When I replaced my code with yours, I lost all functionality of when I enter a single character it goes to the next field and when I tab back it highlights the text in the textbox.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36602296
ok, sorry, please wait help from other EE, you may need to request attention.
0
 

Author Closing Comment

by:us1975mc
ID: 36710339
Your answer worked for what I ask, I just need to ask my question again with all the components that I need.

Thanks!
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question