Validate input text box by key code and selection start - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Input Text

Description

Validate input text box by key code and selection start

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <script>
        function ValidateInput(ctrl) {
            console.log(event.keyCode);
            console.log(ctrl.selectionStart);
            if (event.keyCode == 8 ||event.keyCode == 46) {
                if (ctrl.selectionStart <= 4) 
                    return false;/*from  w  w  w.  jav a 2s  .co  m*/
            }
            return true;
        }
    
      </script> 
   </head> 
   <body> 
      <input type="text" id="Name" name="Name" value="1234" onkeydown="return ValidateInput(this);">  
   </body>
</html>

Related Tutorials