:: Forum >> Version 2 >>

Input validation on keypress

More information on this topic is available in the documentation section: /ui.input/.

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
Saturday, November 4, 2006
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
Tuesday, November 7, 2006
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 {
    
overflowhidden
}

</
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)
Tuesday, November 7, 2006
Awesome... worked perfectly. Thanks.
Dave Delgado
Sunday, November 12, 2006



This topic is archived.

Back to /ui.input/

Documentation:

Forum search