How to validate name using Regex onkeypress in JavaScript

In this article, we will learn how to perform regex onkeypress events in JavaScript. It allows only ASCII Code with Letter (A-Z). when the user types in the textBox, the textbox will be validated using the Onkeypress event handle in JavaScript. If the inputted value is not a character then an error message will be displayed to the text box, it only allows text character.

Ways to Use

There are the following ways to use regex to validate name (text or character).

  1. By using Alphabet Character
  2. By using ASCII Code

1.By using Alphabet Character

Following is the first example, In HTML code textBox okeypress function assigned. which call javaScript function.


2. By using ASCII Code

Following is the second example, in this regex is performed with ASCII Code (A-Z ) Capital Letter is (65 to 90) And Small Letter( a-z) is ( 97-123). it is easy to use only we have to put this code in a