I'm new to javascript. I have an input box that only accepts numbers. If someone copy-paste on that input, it should trim all the non numeric characters. The input field should only be max 10 characters.
Here is the code. It is working in Chrome but not in IE. Can someone figure out what is wrong?
<!DOCTYPE html>
<html>
<body>
<input type="text" maxlength="10" title="Number" pattern="[0-9]*" onkeypress='validate(event)' onpaste="strip(this, event)"/>
<p id="demo"></p>
<script>
function strip(obj, evt) {
var theEvent = evt || window.event;
obj.value = theEvent.clipboardData.getData('Text').replace(/\D/g, '');
obj.value = obj.value.substring(0,Math.min(10,obj.value.length));
theEvent.returnValue = false;
}
function validate(evt) {
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
key = String.fromCharCode(key);
var regex = /[0-9]/;
if (!regex.test(key)) {
theEvent.returnValue = false;
if (theEvent.preventDefault) theEvent.preventDefault();
}
}
</script>
</body>
</html>
Chrome Version - Version 40.0.2214.115 m IE Version - 9.0.8
Thanks!