<!doctype html> <html ng-app> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script> <script> var validateDouble = function validate( evt ) { var theEvent = evt || window.event; // Handle paste if (theEvent.type === 'paste') { key = event.clipboardData.getData('text/plain'); } else { // Handle key press var key = theEvent.keyCode || theEvent.which; key = String.fromCharCode(key); } var curValue = theEvent.target.value; var regex = curValue && curValue.indexOf( '.' ) >= 0 ? /[0-9]/ : /[0-9]|\./; if( !regex.test(key) ) { theEvent.returnValue = false; if(theEvent.preventDefault) theEvent.preventDefault(); } }; document.addEventListener("DOMContentLoaded", function(event) { let input = document.getElementById("input1"); input.setAttribute( 'onkeypress', 'validateDouble(event)' ); input.setAttribute( 'onpaste', 'validateDouble(event)' ); }); </script> </head> <body> <div> <label>Name:</label> <input type="text" id="input1" ng-model="yourName" placeholder="Enter a double"> </div> </body> </html>
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more