Monday, August 6, 2007

Creating edit field for numbers

During working on iRuler.net I had need an HTML input element for numbers only. I haven't found any xHTML solution, so I have appealed to JavaScript. There is the JavaScript solution for limiting possible symbols:


function numberFieldListener(obj){
    var numb = "0123456789";
    var w = "";
    for (var i=0; i < obj.value.length; i++) {
        var x = obj.value.charAt(i);
        if (numb.indexOf(x,0) != -1)
            w += x;
    }
    obj.value = w;
}

<input type="text" onkeyup="numberFieldListener(this)" id="myField"/>
Thus we have following control:

1 comment:

Xantorohara said...

I've used this in my "Hyperbox" project :-)