Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-09-26
7
Medium Priority
?
552 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 83

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 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 83

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Suggested Courses

824 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