Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. <input type="text" id="nameId" onkeydown="validate(this.val)"/>
  2.  
  3. function validate2(val) {
  4. // have no idea how to do.
  5.  
  6. }
  7.  
  8. $("#nameId").on("keydown", function(e) {
  9. // if user writes a char at index == 0 that is not an arrow or HOME and END
  10. if (($(this).get(0).selectionStart == 0 && (e.keyCode < 35 || e.keyCode > 40))
  11. // or if user tries to erase first char
  12. || ($(this).get(0).selectionStart == 1 && e.keyCode == 8)) {
  13. // don't write the character
  14. return false;
  15. }
  16. });
  17.  
  18. // prevent right click
  19. $("#nameId").bind("contextmenu", function(e) {
  20. e.preventDefault();
  21. });
  22.  
  23. <input type="text" id="nameId" value="Given Name" onkeydown="save(this,event)" onkeyup="restore(this,event)" onchange="restore(this,event)"/>
  24.  
  25. function restore(el,event) {
  26. if(el.value.length == 0){
  27. el.value = el.dataset.value.substr(0,1);
  28. }else{
  29. el.dataset.value = el.value;
  30. }
  31. }
  32. function save(el,event) {
  33. var key = event.which || event.charCode || event.keyCode;
  34. if((key === 8 && el.value.length === 1)
  35. || (key === 46 && el.selectionStart == 0 && el.value.length === 1)){
  36. event.preventDefault();
  37. }
  38. if(el.value.length > 0){
  39. el.dataset.value = el.value;
  40. }
  41. }
  42.  
  43. <input type="text" id="nameId" value="Given Name" />
  44.  
  45. var lastentry = '';
  46.  
  47. $("#nameId").on("keyup", function(e) {
  48. var targetValue = $(e.currentTarget).attr('value');
  49. var targetValueLength = targetValue.length;
  50. var inputValue = this.value;
  51.  
  52. if(checkChanges(targetValueLength, targetValue, inputValue))
  53. this.value = targetValue + lastentry;
  54. else
  55. lastentry = this.value.slice(targetValueLength)
  56. });
  57.  
  58. function checkChanges(targetValueLength, targetValue, inputValue)
  59. {
  60. for(var i = 0; i < targetValueLength ; i++)
  61. {
  62. if(targetValue[i] != inputValue[i])
  63. return true;
  64. }
  65. return false;
  66. }
  67.  
  68. <input type="text" id="nameId" value="Given Name" onkeydown="validate(this.value,event)"/>
  69. <script>
  70.  
  71. function validate(val,event) {
  72. // have no idea how to do.
  73. if(event.target.selectionStart != undefined && (event.which === 46 ||event.which === 8)){
  74. var startPos = event.target.selectionStart,
  75. endPos = event.target.selectionEnd;
  76. console.log(startPos,endPos);
  77. if(startPos === 0 && startPos != endPos){
  78. var restPart = val.slice(endPos,val.length);
  79. if(restPart){
  80. val = val[0].concat(restPart);
  81. } else{
  82. val = val[0]
  83. }
  84. event.target.value = val;
  85. event.preventDefault();
  86. } else if(startPos === 0 && startPos === endPos && event.which === 46){
  87. event.preventDefault();
  88. } else if(startPos === 1 && event.which === 8){
  89. event.preventDefault();
  90. }
  91.  
  92. }
  93. }
  94. </script>
  95.  
  96. $(document).keydown(function(e)
  97. {
  98.  
  99. var value = $('#nameId').val().length;
  100.  
  101. if ( e.keyCode == 8 && value < 2)
  102. e.preventDefault();
  103. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement