Guest User

Untitled

a guest
Jul 17th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. <h:inputSecret maxlength="6" value="#{userBean.field}" />
  2.  
  3. <h:form class="form">
  4. <h:inputSecret maxlength="6" value="#{userBean.field}" class="pass"/>
  5. </h:form>
  6.  
  7. //creating mask from real value according to required steps
  8. function createMask(value) {
  9. var mask = "";
  10. var valueLen = value.length;
  11. if (valueLen === 1) {
  12. mask = value;
  13. } else if (valueLen === 2) {
  14. mask = "*" + value.substring(1, 2);
  15. } else if (valueLen === 3) {
  16. mask = "**" + value.substring(2, 3);
  17. } else if (valueLen === 4) {
  18. mask = "**" + value.substring(2, 4);
  19. } else if (valueLen === 5) {
  20. mask = "**" + value.substring(2, 5);
  21. } else if (valueLen === 6) {
  22. mask = "**" + value.substring(2, 4) + "**";
  23. }
  24. //if maxLength>6 then add more else ifs
  25. return mask;
  26. }
  27.  
  28. $(document).ready(function () {
  29. var timer = "";
  30. //add hidden field to hold actual typed value
  31. $(".form").append($('<input type="hidden" class="hidpassw" />'));
  32. //give hidden field the same name as original
  33. $(".hidpassw").attr("name", $(".pass").attr("name"));
  34. //change original type from password to text and remove name property
  35. $(".pass").attr("type", "text").removeAttr("name");
  36. //get maxlength attr value of original field
  37. var maxLength = parseInt($(".pass").attr("maxlength"));
  38.  
  39. //alphanumeric chars are added to value
  40. $("body").on("keypress", ".pass", function (e) {
  41. var code = e.which;
  42. var length = $(".hidpassw").val().length;
  43. //modify regex if it doesnt fit your 'alphanumeric' definition
  44. if (/^[a-zA-Z0-9]$/i.test(String.fromCharCode(code))) {
  45. //check if maxLength is not reached
  46. if (length < maxLength) {
  47. //if length is less them maxLength then add typed alphanumeric char to hidden value
  48. var character = String.fromCharCode(code);
  49. $(".hidpassw").val($(".hidpassw").val() + character);
  50. }
  51. }
  52. });
  53.  
  54. //special case for backspace
  55. $("body").on("keydown", ".pass", function (e) {
  56. var code = e.which;
  57. var length = $(".hidpassw").val().length;
  58. if (code === 8) {
  59. e.preventDefault();
  60. if (length > 0) {
  61. clearTimeout(timer);
  62. $(".hidpassw").val($(".hidpassw").val().substring(0, length - 1));
  63. $(".pass").val(createMask($(".hidpassw").val()));
  64. }
  65. }
  66. });
  67.  
  68.  
  69. $("body").on("keyup", ".pass", function (e) {
  70. var length = $(".hidpassw").val().length;
  71. if (length <= maxLength) {
  72. clearTimeout(timer);
  73. //show last typed char for 100ms and then mask it
  74. timer = setTimeout(function () {
  75. $(".pass").val(createMask($(".hidpassw").val()));
  76. }, 100);
  77. }
  78. });
  79. });
Add Comment
Please, Sign In to add comment