Javascript Keypress Validations

Posted by Jetlogs @ 10:48 am
Category: Web Development, jQuery

Most client-side validation nowadays is usually made through onsubmit validations. Although it is pretty convenient, it may be quite annoying for a user to find only at the end that most of their inputs are invalid and must enter them once again. If onblur validations is not enough, keyperss validation is the next step.

Here is another alternative to the usual onsubmit validations for forms. Instead of validating a form only at the end, why not validate it as the user is typing their input. This is where keypress validations come in. We can capture it through the onkeypress event.

Here are some examples of keypress validations:

Only integers are allowed:


Only alphanumeric characters are allowed:


No spaces are allowed:

Here is an example keypress validation function:

function alphaOnly(evt)
{
	var charCode = (evt.which) ? evt.which : window.event.keyCode;

	if (charCode <= 13)
    {
    	return true;
    }
    else
    {
		var keyChar = String.fromCharCode(charCode);
		var re = /[a-zA-Z]/
		return re.test(keyChar);
	}
}

What this function does is

  1. capture the keypress character entered
  2. determine if the browser is navigator based or not by checking for the which or keyCode attribute
  3. most of the special character codes will be skipped
  4. validate the character entered with a regex pattern

To use a keypress validation for an input element, we must add its onkeypress attribute with the validation function we want:

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

You can also download the Javascript source below:
Download javascript keypress validation script

PS: As I have always said before, client-side validation is no substitute for server-side validation. Client-size validation is more for usability and reducing some load from your server (through reduced rendering of error pages). So don’t be lazy!

UPDATE:
For those who are interested in the jQuery version, here is the sample code:

$("input").bind("keypress", function(evt)
{
    var charCode = (evt.which) ? evt.which : window.event.keyCode; 

    if (charCode <= 13)
    {
        return true;
    }
    else
    {
        var keyChar = String.fromCharCode(charCode);
        var re = /[a-zA-Z]/
        return re.test(keyChar);
    }
}

5 Comments »

5 Comments to “Javascript Keypress Validations”

  1. RaMaNoOb
    1

    thanks man hahaha, why did you not made it into a jquery onkeypress?

  2. Jetlogs
    2
    Author Comment

    Made it plain Javascript first so it can be applied universally. But if you are interested,
    here is the jQuery version:

    $("input").bind("keypress", function(evt)
    {
        var charCode = (evt.which) ? evt.which : window.event.keyCode; 
    
        if (charCode < = 13)
        {
            return true;
        }
        else
        {
            var keyChar = String.fromCharCode(charCode);
            var re = /[a-zA-Z]/
            return re.test(keyChar);
        }
    }

    just replace “input” with your selector for your input field (either through id or class). Using plain “input” will apply the keypress event to all <input…> elements

  3. RaMaNoOb
    3

    DOMO!!!!!

  4. Deva
    4

    its nice… very useful for form validation without alert msg in javascript… my option is suppose if u typing charactors in txt box is shown msg “only integers” near to tat box….. send to soon

  5. Alex
    5

    Nice. Thanks,

    But, how do you use the jquery version ?
    Any advantage ?

RSS Comment Feed Comments RSS |trackback TrackBack URI

Leave a comment

  • Archives

  • Donations

  • Social Bookmarks

  • Jetlogs.org
    Some Rights Reserved 2007
    Creative Commons License