Guest User

Untitled

a guest
Oct 17th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. // validation input //
  2. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3. //validation data in YYMM-format (credit cards)
  4. function chkFormatYYMM(input) {
  5. switch (input.length) {
  6. case 1: {
  7. if (input[0] > 0) {
  8. return true;
  9. } else {
  10. return false;
  11. }
  12. }
  13. case 2: {
  14. var year = input[0] + input[1];
  15. if (year > 16) {
  16. return true;
  17. } else {
  18. return false;
  19. }
  20. }
  21. case 3: {
  22. if (input[2] == 0 || input[2] == 1) {
  23. return true;
  24. } else {
  25. return false;
  26. }
  27. }
  28. case 4: {
  29. var month = input[2] + input[3];
  30. if (month > 0 && month < 13) {
  31. return true;
  32. } else {
  33. return false;
  34. }
  35. }
  36. }
  37. }
  38. //validation when symbol was inputed
  39. var type = "";
  40. function validateInput(input, type) {
  41. switch (type) {
  42. case "string": {
  43. if(input.match(/[^a-zA-Zа-яА-ЯЁё0-9- _]/g)){
  44. return input.slice(0, -1);
  45. }else{
  46. return input;
  47. }break;
  48. }
  49. case "number": {
  50. if(input.match(/[^0-9]/g)){
  51. return input.slice(0, -1);
  52. }else{
  53. return input;
  54. }break;
  55. }
  56. case "YYMM": {
  57. if(input.match(/[^0-9]/g)) {
  58. return input.slice(0, -1);
  59. } else {
  60. if (chkFormatYYMM(input)) {
  61. return input;
  62. } else {
  63. return input.slice(0, -1);
  64. }
  65. }
  66. }
  67. }
  68. }
  69. //listener of inputs are changing
  70. $(':input').on('propertychange input', function(e) {
  71. switch ($(this).attr('id')) {
  72. case "name": {type = "string";break;}
  73. case "expiry": {type = "YYMM";break;}
  74. case "cvv": {type = "number";break;}
  75. case "card": {type = "number";break;}
  76. }
  77. var rez = validateInput(e.target.value, type);
  78. $(this).val(rez);
  79. $(this).focus();
  80. });
  81.  
  82. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Add Comment
Please, Sign In to add comment