Validation for Positive Integer Only Input with regex - Javascript RegExp

Javascript examples for RegExp:Match Number

Description

Validation for Positive Integer Only Input with regex

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body> 
      <form action="javascript:console.log('submitted')"> 
         <input type="text" name="example" id="example"> 
         <button onclick="powerOf(event)">Submit</button> 
      </form> 
      <script>
function powerOf(event) {/*from   w  ww  . ja v a2 s  .  c  om*/
    if (!/^\d+$/.test(document.getElementById('example').value)) {
        event.preventDefault();
    }
}

      </script>  
   </body>
</html>

Related Tutorials