Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // validation input //
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //validation data in YYMM-format (credit cards)
- function chkFormatYYMM(input) {
- switch (input.length) {
- case 1: {
- if (input[0] > 0) {
- return true;
- } else {
- return false;
- }
- }
- case 2: {
- var year = input[0] + input[1];
- if (year > 16) {
- return true;
- } else {
- return false;
- }
- }
- case 3: {
- if (input[2] == 0 || input[2] == 1) {
- return true;
- } else {
- return false;
- }
- }
- case 4: {
- var month = input[2] + input[3];
- if (month > 0 && month < 13) {
- return true;
- } else {
- return false;
- }
- }
- }
- }
- //validation when symbol was inputed
- var type = "";
- function validateInput(input, type) {
- switch (type) {
- case "string": {
- if(input.match(/[^a-zA-Zа-яА-ЯЁё0-9- _]/g)){
- return input.slice(0, -1);
- }else{
- return input;
- }break;
- }
- case "number": {
- if(input.match(/[^0-9]/g)){
- return input.slice(0, -1);
- }else{
- return input;
- }break;
- }
- case "YYMM": {
- if(input.match(/[^0-9]/g)) {
- return input.slice(0, -1);
- } else {
- if (chkFormatYYMM(input)) {
- return input;
- } else {
- return input.slice(0, -1);
- }
- }
- }
- }
- }
- //listener of inputs are changing
- $(':input').on('propertychange input', function(e) {
- switch ($(this).attr('id')) {
- case "name": {type = "string";break;}
- case "expiry": {type = "YYMM";break;}
- case "cvv": {type = "number";break;}
- case "card": {type = "number";break;}
- }
- var rez = validateInput(e.target.value, type);
- $(this).val(rez);
- $(this).focus();
- });
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Add Comment
Please, Sign In to add comment