How to prevent invalid characters from being typed into input fields

后端 未结 7 1715
猫巷女王i
猫巷女王i 2020-11-29 10:11

Onkeydown, I run the following JavaScript:

function ThisOnKeyDown(el) {
   if (el.title == \'textonly\') {
       !(/^[A-Za-zÑñ-\\s]*$/i).test(e         


        
相关标签:
7条回答
  • 2020-11-29 11:01

    i found this solution in: http://help.dottoro.com/ljlkwans.php

    works as intended.

    <script type="text/javascript">
        function FilterInput (event) {
            var keyCode = ('which' in event) ? event.which : event.keyCode;
    
            isNumeric = (keyCode >= 48 /* KeyboardEvent.DOM_VK_0 */ && keyCode <= 57 /* KeyboardEvent.DOM_VK_9 */) ||
                        (keyCode >= 96 /* KeyboardEvent.DOM_VK_NUMPAD0 */ && keyCode <= 105 /* KeyboardEvent.DOM_VK_NUMPAD9 */);
            modifiers = (event.altKey || event.ctrlKey || event.shiftKey);
            return !isNumeric || modifiers;
        }
    </script>
    
    < body>
    The following text field does not accept numeric input:
    <input type="text" onkeydown="return FilterInput (event)" />< /body>
    

    it allows text and !"#$%& but you can adjust it adding these to the validationto only allow numbers by removing the ! in the return

    0 讨论(0)
提交回复
热议问题