Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $('.creditCardText').keyup(function() {
- var cardValue = $('.creditCardText').val(),
- cardLength = cardValue.length;
- if ( cardLength < 5 ) {
- if ( cardLength % 4 == 0 ) {
- console.log('4 lük geldi');
- cardValue += "-";
- $('.creditCardText').val(cardValue);
- }
- } else {
- if ( cardLength % 5 == 0 ) {
- console.log('5 lük geldi');
- cardValue += "-";
- $('.creditCardText').val(cardValue);
- }
- }
- });
- $("#credit").mask("9999-9999-9999-9999");
- $("#credit").mask("9999-9999-9999-99?99");
- $('.creditCardText').keyup(function() {
- var foo = $(this).val().split("-").join(""); // remove hyphens
- if (foo.length > 0) {
- foo = foo.match(new RegExp('.{1,4}', 'g')).join("-");
- }
- $(this).val(foo);
- });
- document.querySelector('.creditCardText').addEventListener('input', function (e) {
- var foo = this.value.split("-").join("");
- if (foo.length > 0) {
- foo = foo.match(new RegExp('.{1,4}', 'g')).join("-");
- }
- this.value = foo;
- });
- input_credit_card = function(input)
- {
- var format_and_pos = function(char, backspace)
- {
- var start = 0;
- var end = 0;
- var pos = 0;
- var separator = "-";
- var value = input.value;
- if (char !== false)
- {
- start = input.selectionStart;
- end = input.selectionEnd;
- if (backspace && start > 0) // handle backspace onkeydown
- {
- start--;
- if (value[start] == separator)
- { start--; }
- }
- // To be able to replace the selection if there is one
- value = value.substring(0, start) + char + value.substring(end);
- pos = start + char.length; // caret position
- }
- var d = 0; // digit count
- var dd = 0; // total
- var gi = 0; // group index
- var newV = "";
- var groups = /^D*3[47]/.test(value) ? // check for American Express
- [4, 6, 5] : [4, 4, 4, 4];
- for (var i = 0; i < value.length; i++)
- {
- if (/D/.test(value[i]))
- {
- if (start > i)
- { pos--; }
- }
- else
- {
- if (d === groups[gi])
- {
- newV += separator;
- d = 0;
- gi++;
- if (start >= i)
- { pos++; }
- }
- newV += value[i];
- d++;
- dd++;
- }
- if (d === groups[gi] && groups.length === gi + 1) // max length
- { break; }
- }
- input.value = newV;
- if (char !== false)
- { input.setSelectionRange(pos, pos); }
- };
- input.addEventListener('keypress', function(e)
- {
- var code = e.charCode || e.keyCode || e.which;
- // Check for tab and arrow keys (needed in Firefox)
- if (code !== 9 && (code < 37 || code > 40) &&
- // and CTRL+C / CTRL+V
- !(e.ctrlKey && (code === 99 || code === 118)))
- {
- e.preventDefault();
- var char = String.fromCharCode(code);
- // if the character is non-digit
- // OR
- // if the value already contains 15/16 digits and there is no selection
- // -> return false (the character is not inserted)
- if (/D/.test(char) || (this.selectionStart === this.selectionEnd &&
- this.value.replace(/D/g, '').length >=
- (/^D*3[47]/.test(this.value) ? 15 : 16))) // 15 digits if Amex
- {
- return false;
- }
- format_and_pos(char);
- }
- });
- // backspace doesn't fire the keypress event
- input.addEventListener('keydown', function(e)
- {
- if (e.keyCode === 8 || e.keyCode === 46) // backspace or delete
- {
- e.preventDefault();
- format_and_pos('', this.selectionStart === this.selectionEnd);
- }
- });
- input.addEventListener('paste', function()
- {
- // A timeout is needed to get the new value pasted
- setTimeout(function(){ format_and_pos(''); }, 50);
- });
- input.addEventListener('blur', function()
- {
- // reformat onblur just in case (optional)
- format_and_pos(this, false);
- });
- };
- input_credit_card(document.getElementById('credit-card'));
- $('.creditCardText').keyup(function() {
- var ss, se, obj;
- obj = $(this);
- ss = obj[0].selectionStart;
- se = obj[0].selectionEnd;
- var curr = obj.val();
- var foo = $(this).val().split("-").join(""); // remove hyphens
- if (foo.length > 0) {
- foo = foo.match(new RegExp('.{1,4}', 'g')).join("-");
- }
- if(( (curr.length % 5 == 0) && ss == se && ss == curr.length ) || (ss == se && (ss % 5 == 0))){
- ss += 1;
- se += 1;
- }
- if (curr != foo){
- $(this).val(foo);
- obj[0].selectionStart = ss;
- obj[0].selectionEnd = se;
- }
- });
- //Html
- /* <input type="text" class="creditCardText" maxlength="19" /> */
- $('.creditCardText').keyup(function () {
- var cctlength = $(this).val().length; // get character length
- switch (cctlength) {
- case 4:
- var cctVal = $(this).val();
- var cctNewVal = cctVal + '-';
- $(this).val(cctNewVal);
- break;
- case 9:
- var cctVal = $(this).val();
- var cctNewVal = cctVal + '-';
- $(this).val(cctNewVal);
- break;
- case 14:
- var cctVal = $(this).val();
- var cctNewVal = cctVal + '-';
- $(this).val(cctNewVal);
- break;
- default:
- break;
- }
- });
Add Comment
Please, Sign In to add comment