Skip to main content
deleted 9 characters in body
Source Link
j08691
  • 208.5k
  • 33
  • 269
  • 281

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!

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!

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

Source Link
racumin
  • 402
  • 2
  • 4
  • 18

Javascript working in Chrome but not in IE

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!