3.2.0

Input validation on keypress

Hi,

Is there anyway to validate each key used on input? I'd like to restrict a field to only numeric values and would like to just block alpha characters and not alert or prompt, just 'ignore'.

TIA!

-- Dave
Dave Delgado
November 4,
And this is as good as it gets:

activeInput.onKeyPress = function (event) {

var isIe = /msie/i.test(navigator.userAgent);
var key = String.fromCharCode(event.keyCode || event.charCode);
var txt = this.getControlText();

if ('0123456789'.indexOf(key) == -1)
{
if (isIe)
event.keyCode = 0;
else
{
event.preventDefault();
event.stopPropagation();
}
}

}

Rodrigo
November 7,
Here is another solution using onControlTextChanging event (also works on copy/paste etc.)

<style>

/* fix FF missing oninput event bug */
.aw-gecko .aw-edit-control .aw-item-box {
    overflow: hidden; 
}

</style>
<script>

    var obj = new AW.UI.Input;

    obj.onControlTextChanging = function(text){
        if (text.match(/[^0-9.+-]/)){
            return "error"; // prevent non-digits
        }
    }

    document.write(obj);

</script>
Alex (ActiveWidgets)
November 7,
Awesome... worked perfectly. Thanks.
Dave Delgado
November 12,

This topic is archived.

See also:


Back to support forum