tags:

views:

47

answers:

3

how we can restrict a character to type in a text box.

A: 

HTML is markup language, to achieve what you are looking for you will need to use javascript. Also you might try googling.

Darin Dimitrov
+1  A: 

If you have the text box then you have to handle the onkeypress event

<input type='text' onkeypress='keypresshandler(event)' />

You can use the following function to restrict the users

    function keypresshandler(event)
    {
         var charCode = event.keyCode;
         //Non-numeric character range
         if (charCode > 31 && (charCode < 48 || charCode > 57))
         return false;
    }
Multiplexer
We do it like this :)
Znarkus
+3  A: 

You can do this via javascript (so if javascript is off, you won't be able to restrict it)

<input type="text" onkeyup="this.value = this.value.replace(/[^a-z]/, '')" />

This will restrict it to only a-z characters. Checkout regular expressions to see what you can do

Ben Rowe
Remember that this code only executes on the client side. The server script must be prepared to handle **any** input and reject invalid characters.
In silico