Codice PHP:<script type="text/javascript">
function $(id){
return document.getElementById(id);
};
function getTarget( evt ){
evt = evt || window.event || null;
return evt?(evt.target||evt.srcElement||null):null;
};
function stripNonNumeric(str) {
str += '';
var rgx = /^\d|\.|-$/;
var out = '';
for( var i = 0; i < str.length; i++ ){
if( rgx.test( str.charAt(i) ) ){
if( !( ( str.charAt(i) == '.' && out.indexOf( '.' ) != -1 ) || ( str.charAt(i) == '-' && out.length != 0 ) ) ){
out += str.charAt(i);
}
}
}
return out;
} // stripNonNumeric()
var ctrlIsNumeric = function(e) {
var ret = stripNonNumeric(getTarget(e).value);
if (ret != '') getTarget(e).value = ret;
}
function addEvent(elm, evType, fn, useCapture){
if (elm.addEventListener){
elm.addEventListener(evType, fn, useCapture);
return true;
} else if (elm.attachEvent) {
var r = elm.attachEvent('on' + evType, fn);
return r;
} else {
elm['on' + evType] = fn;
}
};
function init(){
addEvent($('rg'), 'keyup', ctrlIsNumeric);
}
addEvent(window,'load',init,false);
</script>
</head>
<body>
<input id="rg" type="text" value="" />
</body>
</html>
![]()


Rispondi quotando