Deny entering a character in the text box

how can we limit the input of a character in a text field.

+5
source share
5 answers

You can do this via javascript (therefore, if javascript is turned off, you cannot restrict it)

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

This will limit it to az characters only. Checkout regular expressions to see what you can do

+14
source

If you have a text box, you need to handle the event onkeypress

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

You can use the following function to restrict users

    function keypresshandler(event)
    {
         var charCode = event.keyCode;
         //Non-numeric character range
         if (charCode > 31 && (charCode < 48 || charCode > 57))
         return false;
    }
+6
source

, HTML5 JavaScript- .

<input type="text" name="text" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$"> RegExp ( : ).

title /, , .

<form action="/add_country.php">
  Country code: <input type="text" name="country_code" pattern="[A-Za-z]{3}" title="Three letter country code">
  <input type="submit">
</form>

. HTML. (, Safari).

+2
   function isNumberKey1(evt)
{
       var charCode = (evt.which) ? evt.which : event.keyCode;
      if ( char!=8(charCode < 65 || charCode > 106))
         return false;

         return true;
}
+1

- . . , oninput onkeyup:

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

All Articles