?
Solved

allowed chars in Javascript

Posted on 2012-04-03
3
Medium Priority
?
245 Views
Last Modified: 2012-08-13
Greetings

I am trying to limit characters that can be typed in the textbox on an asp.net web page. Only numbers 0-9 and the two alphabets S and T are allowed. small letter s and t can be allowed but changed to uppercase on keypress. Below is what I tried but it doesn't work.

Please assist!

MandyProg
Allowed-keys.txt
0
Comment
Question by:MandyProgza
[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 Comments
 
LVL 20

Accepted Solution

by:
Proculopsis earned 448 total points
ID: 37801988
Take a look at this jsfiddle.

The code is:

$("#test").keyup(function() {
    var value = $(this).val().toUpperCase();
    value = value.replace(/[^0-9ST]/g, "");
    if ($(this).val() != value) $(this).val(value);
});

Open in new window

0
 

Author Comment

by:MandyProgza
ID: 37804803
Thanks, but what if I want to check if the first character is either S or T or 1?

Can you modify the code for that?
0
 
LVL 7

Assisted Solution

by:Atique Ansari
Atique Ansari earned 452 total points
ID: 37809815
Above code does replace the code with blank value after accepting it. Its visible on screen that character is removed after being entered in text box.

Go to below URL.

http://jsfiddle.net/attu143/6pMeL/
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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

800 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