Guest User

Untitled

a guest
Dec 13th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. $('.creditCardText').keyup(function() {
  2.  
  3. var cardValue = $('.creditCardText').val(),
  4. cardLength = cardValue.length;
  5.  
  6. if ( cardLength < 5 ) {
  7. if ( cardLength % 4 == 0 ) {
  8. console.log('4 lük geldi');
  9. cardValue += "-";
  10. $('.creditCardText').val(cardValue);
  11. }
  12. } else {
  13. if ( cardLength % 5 == 0 ) {
  14. console.log('5 lük geldi');
  15. cardValue += "-";
  16. $('.creditCardText').val(cardValue);
  17.  
  18. }
  19. }
  20.  
  21. });
  22.  
  23. $("#credit").mask("9999-9999-9999-9999");
  24.  
  25. $("#credit").mask("9999-9999-9999-99?99");
  26.  
  27. $('.creditCardText').keyup(function() {
  28. var foo = $(this).val().split("-").join(""); // remove hyphens
  29. if (foo.length > 0) {
  30. foo = foo.match(new RegExp('.{1,4}', 'g')).join("-");
  31. }
  32. $(this).val(foo);
  33. });
  34.  
  35. document.querySelector('.creditCardText').addEventListener('input', function (e) {
  36. var foo = this.value.split("-").join("");
  37. if (foo.length > 0) {
  38. foo = foo.match(new RegExp('.{1,4}', 'g')).join("-");
  39. }
  40. this.value = foo;
  41. });
  42.  
  43. input_credit_card = function(input)
  44. {
  45. var format_and_pos = function(char, backspace)
  46. {
  47. var start = 0;
  48. var end = 0;
  49. var pos = 0;
  50. var separator = "-";
  51. var value = input.value;
  52.  
  53. if (char !== false)
  54. {
  55. start = input.selectionStart;
  56. end = input.selectionEnd;
  57.  
  58. if (backspace && start > 0) // handle backspace onkeydown
  59. {
  60. start--;
  61.  
  62. if (value[start] == separator)
  63. { start--; }
  64. }
  65. // To be able to replace the selection if there is one
  66. value = value.substring(0, start) + char + value.substring(end);
  67.  
  68. pos = start + char.length; // caret position
  69. }
  70.  
  71. var d = 0; // digit count
  72. var dd = 0; // total
  73. var gi = 0; // group index
  74. var newV = "";
  75. var groups = /^D*3[47]/.test(value) ? // check for American Express
  76. [4, 6, 5] : [4, 4, 4, 4];
  77.  
  78. for (var i = 0; i < value.length; i++)
  79. {
  80. if (/D/.test(value[i]))
  81. {
  82. if (start > i)
  83. { pos--; }
  84. }
  85. else
  86. {
  87. if (d === groups[gi])
  88. {
  89. newV += separator;
  90. d = 0;
  91. gi++;
  92.  
  93. if (start >= i)
  94. { pos++; }
  95. }
  96. newV += value[i];
  97. d++;
  98. dd++;
  99. }
  100. if (d === groups[gi] && groups.length === gi + 1) // max length
  101. { break; }
  102. }
  103. input.value = newV;
  104.  
  105. if (char !== false)
  106. { input.setSelectionRange(pos, pos); }
  107. };
  108.  
  109. input.addEventListener('keypress', function(e)
  110. {
  111. var code = e.charCode || e.keyCode || e.which;
  112.  
  113. // Check for tab and arrow keys (needed in Firefox)
  114. if (code !== 9 && (code < 37 || code > 40) &&
  115. // and CTRL+C / CTRL+V
  116. !(e.ctrlKey && (code === 99 || code === 118)))
  117. {
  118. e.preventDefault();
  119.  
  120. var char = String.fromCharCode(code);
  121.  
  122. // if the character is non-digit
  123. // OR
  124. // if the value already contains 15/16 digits and there is no selection
  125. // -> return false (the character is not inserted)
  126.  
  127. if (/D/.test(char) || (this.selectionStart === this.selectionEnd &&
  128. this.value.replace(/D/g, '').length >=
  129. (/^D*3[47]/.test(this.value) ? 15 : 16))) // 15 digits if Amex
  130. {
  131. return false;
  132. }
  133. format_and_pos(char);
  134. }
  135. });
  136.  
  137. // backspace doesn't fire the keypress event
  138. input.addEventListener('keydown', function(e)
  139. {
  140. if (e.keyCode === 8 || e.keyCode === 46) // backspace or delete
  141. {
  142. e.preventDefault();
  143. format_and_pos('', this.selectionStart === this.selectionEnd);
  144. }
  145. });
  146.  
  147. input.addEventListener('paste', function()
  148. {
  149. // A timeout is needed to get the new value pasted
  150. setTimeout(function(){ format_and_pos(''); }, 50);
  151. });
  152.  
  153. input.addEventListener('blur', function()
  154. {
  155. // reformat onblur just in case (optional)
  156. format_and_pos(this, false);
  157. });
  158. };
  159.  
  160. input_credit_card(document.getElementById('credit-card'));
  161.  
  162. $('.creditCardText').keyup(function() {
  163.  
  164. var ss, se, obj;
  165. obj = $(this);
  166. ss = obj[0].selectionStart;
  167. se = obj[0].selectionEnd;
  168.  
  169. var curr = obj.val();
  170.  
  171. var foo = $(this).val().split("-").join(""); // remove hyphens
  172. if (foo.length > 0) {
  173. foo = foo.match(new RegExp('.{1,4}', 'g')).join("-");
  174. }
  175.  
  176. if(( (curr.length % 5 == 0) && ss == se && ss == curr.length ) || (ss == se && (ss % 5 == 0))){
  177. ss += 1;
  178. se += 1;
  179. }
  180.  
  181. if (curr != foo){
  182. $(this).val(foo);
  183. obj[0].selectionStart = ss;
  184. obj[0].selectionEnd = se;
  185. }
  186.  
  187. });
  188.  
  189. //Html
  190. /* <input type="text" class="creditCardText" maxlength="19" /> */
  191. $('.creditCardText').keyup(function () {
  192. var cctlength = $(this).val().length; // get character length
  193.  
  194. switch (cctlength) {
  195. case 4:
  196. var cctVal = $(this).val();
  197. var cctNewVal = cctVal + '-';
  198. $(this).val(cctNewVal);
  199. break;
  200. case 9:
  201. var cctVal = $(this).val();
  202. var cctNewVal = cctVal + '-';
  203. $(this).val(cctNewVal);
  204. break;
  205. case 14:
  206. var cctVal = $(this).val();
  207. var cctNewVal = cctVal + '-';
  208. $(this).val(cctNewVal);
  209. break;
  210. default:
  211. break;
  212. }
  213. });
Add Comment
Please, Sign In to add comment