Alphabets Validation on textbox in JS onkeypress

Alphabets Validation on textbox in JS onkeypress

In this example, alphabet validation in Javascript “onkeypress” event to accept alphabets only.

Alphabets Validation on textbox in JS onkeypress
  • The onkeypress=”return onlyAlphabetsAccept(event,this);” function called in text box on onkeypress event.
  • The charCode checks ASCII code of keys from keyboard.
  • No need for any jQuery library.
<!DOCTYPE html>
<html>
    <head>
    <script type="text/javascript">
    function onlyAlphabetsAccept(e, t) {
        try {
            if (window.event) { var charCode = window.event.keyCode; }
            else if (e) { var charCode = e.which; }
            else { return true; }
            if ((charCode < 65 || charCode > 90) && (charCode < 97 || charCode > 123) && charCode != 32 && charCode != 8 && charCode != 46) {
                return false;
            }
            return true;
        }
        catch (err) { alert(err.Description); }
    }
    </script>
   </head>
	<body>

     <input type="text" onkeypress="return onlyAlphabetsAccept(event,this);" name="studentname">

	</body>
</html>

Email Validation onblur in JavaScript … Click here …

Alphabets Validation on textbox in JS onkeypress

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *