Solved

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

Posted on 2011-09-26
7
541 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

821 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