12.1.06 Simple JavaScript Form Field onKeyDown Regular Expression Wrapper

UPDATED —-

This is the final version I’ve been using in the live environment - streamlined and simple for use in windows, mac - IE, FireFox, Safari.

/* onKeyPress validation function */
function validVal(event, keyRE) {
    if (    ( typeof(event.keyCode) != 'undefined' && event.keyCode > 0 && String.fromCharCode(event.keyCode).search(keyRE) != (-1) ) ||
        ( typeof(event.charCode) != 'undefined' && event.charCode > 0 && String.fromCharCode(event.charCode).search(keyRE) != (-1) ) ||
        ( typeof(event.charCode) != 'undefined' && event.charCode != event.keyCode && typeof(event.keyCode) != 'undefined' && event.keyCode.toString().search(/^(8|9|13|45|46|35|36|37|39)$/) != (-1) ) ||
        ( typeof(event.charCode) != 'undefined' && event.charCode == event.keyCode && typeof(event.keyCode) != 'undefined' && event.keyCode.toString().search(/^(8|9|13)$/) != (-1) ) ) {
        return true;
    } else {
        return false;
    }
}

Download this code: new_validation.js

OLD WAY OF DOING THINGS —-

A simple method of data entry consistency is to prevent the end-user from entering in invalid characters in a specific form field. A great deal of expense can be saved by performing this all within JavaScript on the client’s machine - as has been proven and been accepted practice for years. I’ve been finding restricting the characters on a specific form field as the user hits every key a more accepted practice; i.e. It’s all over the place.

This restricting characters method is also supported functionality on a TextField datatype in Flash. Since all the work as of late has been with building RIA’s in both AJAX and Flash, I attempt to take the best feature set for a specific “widget” of the application and duplicate it in the other format. Case in point, duplicating the TextField.restrict functionality within the browser.

Here’s a very simple function to capture each onKeyDown event from an input of type=text. It will check the individual key press against the supplied RegEx in the arguments. It will also ignore control characters as follows:

And the code:

<head>

<script>

    function reKeyPress(event, keyRE) {
        var strr = "";
        for(var i in event) {
            strr += "\n" + i + " - " + event[i];
        }
        document.testing.eventBox.value = strr;
        document.testing.cCodePress.value = event.charCode;
        document.testing.kCodePress.value = event.keyCode;

        if (    ( typeof(event.keyCode) != 'undefined' && event.keyCode > 0 && String.fromCharCode(event.keyCode).search(keyRE) != (-1) ) ||
            ( typeof(event.charCode) != 'undefined' && event.charCode > 0 && String.fromCharCode(event.charCode).search(keyRE) != (-1) ) ||
            ( typeof(event.charCode) != 'undefined' && event.charCode != event.keyCode && typeof(event.keyCode) != 'undefined' && event.keyCode.toString().search(/^(8|9|13|45|46|35|36|37|39)$/) != (-1) ) ||
            ( typeof(event.charCode) != 'undefined' && event.charCode == event.keyCode && typeof(event.keyCode) != 'undefined' && event.keyCode.toString().search(/^(8|9|13)$/) != (-1) ) ) {
            document.testing.pressValid.value = 'true';
            return true;
        } else {
            document.testing.pressValid.value = 'false';
            return false;
        }
    }
    function keyDownHandler(event) {
        document.testing.cCodeDown.value = event.charCode;
        document.testing.kCodeDown.value = event.keyCode;
    }

</script>

</head>

<form name="testing">

north american 10digit phone number: <input type="text" size=10 maxlength=10 onKeyPress="return reKeyPress(event, /[0-9]/);" onKeyDown="keyDownHandler(event);">

<table>
    <tr>
        <td colspan=3>
            <textarea cols=50 rows=20 name="eventBox"></textarea>
        </td>
    </tr>
    <tr>
        <td></td>
        <td>onKeyPress</td>
        <td>onKeyDown</td>
    </tr>
    <tr>
        <td>charCode</td>
        <td><input type="text" size=10 maxlength=10 name="cCodePress" value=""></td>
        <td><input type="text" size=10 maxlength=10 name="cCodeDown" value=""></td>
    </tr>
    <tr>
        <td>keyCode</td>
        <td><input type="text" size=10 maxlength=10 name="kCodePress" value=""></td>
        <td><input type="text" size=10 maxlength=10 name="kCodeDown" value=""></td>
    </tr>
    <tr>
        <td>passes?</td>
        <td><input type="text" size=10 maxlength=10 name="pressValid" value=""></td>
        <td><input type="text" size=10 maxlength=10 name="downValid" value=""></td>
    </tr>
</table>

</form>

Download this code: phone_validation.html


4 Comments
sachin @ 7.31.07 10am

its gr8!!!
helped me 2 much
thanx!!!!!

#319 40chars
Marc Fathauer @ 7.3.08 2pm

So, how would you allow for using this with names which may have “-” , ” ‘ “, or even spaces. IE “Jim Bob” “Bo-Peep” or “O’Brian”?

#402 130chars
Jim Palmer @ 7.14.08 6pm

for the “-”, ” ” and “‘” characters you simply need to change the 2nd argument in the reKeyPress function. This argument is a javascript regular expression, hence the “/” delimiters, and it checks the regular expression supplied against each character entered… not the entire value of the text input field.

Here’s how I’d change the regular expression keyRE argument to support the ['- ] characters:

onKeyPress="return reKeyPress(event, /[a-zA-Z0-9 '-]/);"
#406 475chars
Thibault @ 8.11.08 5am

Thank you ! That’s very useful.

But I always laugh when I read things like :
if ( ) return true; else return false;
You should write return … that’s quite more easy to read.

#414 181chars



monetary contributions are always accepted $
0.577s