I have in my webpage a form called 'contact_form' and I have in it a textarea where I want to allow to type only numbers inside. How do I check it in submission with Javascript?
Thanks in advance.
I have in my webpage a form called 'contact_form' and I have in it a textarea where I want to allow to type only numbers inside. How do I check it in submission with Javascript?
Thanks in advance.
HTML:
<textarea id="text"></textarea>
JavaScript:
var re=/\d/,
    allowedCodes = [37, 39, 8, 9], // left and right arrows, backspace and tab
    text = document.getElementById('text');
text.onkeydown = function(e) {
    var code;
    if(window.event) { // IE8 and earlier
        code = e.keyCode;
    } else if(e.which) { // IE9/Firefox/Chrome/Opera/Safari
        code = e.which;
    }
    if(allowedCodes.indexOf(code) > -1) {
        return true;
    }
    return !e.shiftKey && re.test(String.fromCharCode(code));
};
