Let the user enter only three different characters in the input text box

Is there a way to allow the user to enter only three characters? For example, I want to allow only 3 , 6 or 9 in the input text box.

+6
javascript html
May 25 '11 at 21:21
source share
7 answers

Here you can do it with jQuery: http://jsfiddle.net/ThiefMaster/UZJn2/

 <input type="text" maxlength="1" class="only369" /> $('.only369').keyup(function(e) { if(this.value != '3' && this.value != '6' && this.value != '9') { this.value = ''; } }); 
+4
May 25 '11 at 21:26
source share

Pure JS solution:

http://jsfiddle.net/ampersand/eg8Dn/

Source:

 <input id="number" type="number" min="3" max="9" step="3" value="3"> <script> document.getElementById('number').addEventListener('keyup',function(ev){ this.value=!!~['3','6','9'].indexOf(this.value) ? this.value : ''; }) </script> 
+2
May 25 '11 at 23:12
source share

I think this may help you ...

 <script type="text/javascript"> function validate(e) { return (e.charCode == 51 || e.charCode == 54 || e.charCode == 57) } </script> <form> <input name="name" type="text" onkeypress="return validate(event);" /> </form> 

If you want to limit the number of occurrences, simply set the maxlength property for the input field.

+1
May 25 '11 at 21:36
source share

Short answer:

 "0123456789".replace(/[^369]/g, "") 
0
May 25 '11 at 21:25
source share

Sure. Listen for the onchange event on the input element and when the value is different from these three numbers and more than a tree, return false .

0
May 25 '11 at 21:25
source share

Use onchange="this.value=this.value.replace(/[^369]/g, '')"

0
May 25 '11 at 21:27
source share

Use this plugin. This is FANTASTIC : Alphanumeric

The answer to your question is as simple as this

 $('.numericInput').numeric({ichars:'0124578'}); 

From the authors:

Have you ever had a need to prevent users from entering certain characters in your form?

Looking at the plugins available on jQuery, I found a great plugin created by Sam Collet called Numeric.

But it was too limited, what if I ask the user to create a username? Or what if I need to enter a decimal number or IP address? There is another great plugin called Masked Josh Bush's Input, which can also control user input by defining a mask. However, the problem was that the length of the input text should also be determined. Again, what if I need to control the username input? I cannot say how many characters the user will use, and I cannot force him to use only 8 characters, so I created AlphaNumeric.

jQuery AlphaNumeric is a javascript control plugin that allows you to limit what characters a user can enter into text fields or text fields. Enjoy.

0
May 25 '11 at 21:34
source share



All Articles