Restrict HTML input to only allow paste

后端 未结 3 1904
孤街浪徒
孤街浪徒 2020-12-19 11:58

Is it possible to have a HTML input on a form that only accepts pasted input?

As part of our registration process, the enduser needs to enter a 20 character identifi

相关标签:
3条回答
  • 2020-12-19 12:43
    <script type="text/javascript">
     function validate(evt) {
       var theEvent = evt || window.event;
       var key = theEvent.keyCode || theEvent.which;
       key = String.fromCharCode(key);
      var regex = /[]|\./;
       if(!regex.test(key)) {
        theEvent.returnValue = false;
        if(theEvent.preventDefault) theEvent.preventDefault();
      }
     }
     </script>
      <span>Textbox name</span>
     <input name="ReceiveNo" type="text" class="txtbox" onkeypress='validate(event)' value=""         required tabindex="" />
    
    0 讨论(0)
  • 2020-12-19 12:44

    Why make them even paste it in? if you are sending the verification token via email then just route them to http://example.com/verify/{{TOKEN}} via email and pickup the rest of the registration process from there.

    0 讨论(0)
  • 2020-12-19 12:48

    Here is a more robust solution, I expanded on the code above. There may be a bit of overkill but it works on all browsers. The code below will:

    1. Make the textbox just like readonly but will honor tab index and set focus
    2. Support all clipboard functions win and mac with mouse or keyboard
    3. Allow undo, redo and select all

    $( "#your_textbox" ).keypress(function(evt) {
    	// Note this could be a bit of overkill but I found some
    	// problems in Firefox and decided to go the distance
    	// including old windows keyboard short cut keys.
    	// Enumerate all supported clipboard, undo and redo keys
    	var clipboardKeys = {
    		winInsert : 45,
    		winDelete : 46,
    		SelectAll : 97,
    		macCopy : 99,
    		macPaste : 118,
    		macCut : 120,
    		redo : 121,	
    		undo : 122
    	}
    	// Simulate readonly but allow all clipboard, undo and redo action keys
    	var charCode = evt.which;
    	//alert(charCode);
    	// Accept ctrl+v, ctrl+c, ctrl+z, ctrl+insert, shift+insert, shift+del and ctrl+a
    	if (
    		evt.ctrlKey && charCode == clipboardKeys.redo ||		/* ctrl+y redo			*/
    		evt.ctrlKey && charCode == clipboardKeys.undo ||		/* ctrl+z undo			*/
    		evt.ctrlKey && charCode == clipboardKeys.macCut ||		/* ctrl+x mac cut		*/
    		evt.ctrlKey && charCode == clipboardKeys.macPaste ||		/* ctrl+v mac paste		*/
    		evt.ctrlKey && charCode == clipboardKeys.macCopy ||		/* ctrl+c mac copy		*/ 
    		evt.shiftKey && evt.keyCode == clipboardKeys.winInsert ||	/* shift+ins windows paste	*/ 
    		evt.shiftKey && evt.keyCode == clipboardKeys.winDelete ||	/* shift+del windows cut	*/ 
    		evt.ctrlKey && evt.keyCode == clipboardKeys.winInsert  ||	/* ctrl+ins windows copy	*/ 
    		evt.ctrlKey && charCode == clipboardKeys.SelectAll		/* ctrl+a select all		*/
    		){ return 0; }
    	// Shun all remaining keys simulating readonly textbox
    	var theEvent = evt || window.event;
    	var key = theEvent.keyCode || theEvent.which;
    	key = String.fromCharCode(key);
    	var regex = /[]|\./;
    	if(!regex.test(key)) {
    		theEvent.returnValue = false;
    		theEvent.preventDefault();
    	}
    });

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