Solved

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

Posted on 2011-09-26
7
536 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
Comment Utility
var isWordcharacter = /[a-z\d\-\s']/i.test( c );
0
 
LVL 12

Expert Comment

by:jagssidurala
Comment Utility
0
 

Author Comment

by:us1975mc
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
ok, sorry, please wait help from other EE, you may need to request attention.
0
 

Author Closing Comment

by:us1975mc
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now