Solved

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

Posted on 2011-09-26
7
545 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This article discusses how to implement server side field validation and display customized error messages to the client.
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

632 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