Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Let user type only three different characters in an input text field

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

like image 554
Ahmed Ala Dali Avatar asked May 25 '11 21:05

Ahmed Ala Dali


People also ask

How do I limit characters in a text field?

Right-click the text box for which you want to limit characters, and then click Text Box Properties on the shortcut menu. Click the Display tab. Under Options, select the Limit text box to check box, and then specify the number of characters that you want.

How do I allow only characters in a text box in HTML?

You can write a JavaScript form validation script to check whether the required field(s) in the HTML form contains only letters. To get a string contains only letters (both uppercase or lowercase) we use a regular expression (/^[A-Za-z]+$/) which allows only letters.

How do you prevent a user from removing the first three characters in a text input?

Option 1 : Put the 3 character prefix outside of the input. This is the best from a user experience perspective. Option 2 : Check for backspace keypress and prevent the delete accordingly.


1 Answers

Here's a way to do it using 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 = '';   
    }
});
like image 164
ThiefMaster Avatar answered Oct 28 '22 16:10

ThiefMaster