Advertisement
Guest User

Untitled

a guest
May 20th, 2019
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. // --------------------------------------------------------------------------------------
  2. // this method will validate only words not
  3. function string_spaces(str) {
  4. var isValid = true;
  5. var validChars = "abcdefghijklmnopqrstuvwxyz ";
  6. var charIndex;
  7. if ( str.length < 1 ) { return false ; }
  8. else {
  9. for (charIndex = 0; charIndex < str.length;charIndex++) {
  10. if ( validChars.indexOf( str.charAt(charIndex).toLowerCase() ) < 0 ) {
  11. isValid = false; break;
  12. }
  13. }
  14. }
  15. if ( isValid == true ) return true; else return false;
  16. }
  17. // --------------------------------------------------------------------------------------
  18. // this method will validate only numeric values.
  19. function all_number(str) {
  20. var isValid = true;
  21. var validChars = "0123456789";
  22. var charIndex;
  23. if ( str.length < 1 ) { return false ; }
  24. else {
  25. for (charIndex = 0; charIndex < str.length;charIndex++) {
  26. if ( validChars.indexOf( str.charAt(charIndex).toLowerCase() ) < 0 ) {
  27. isValid = false;
  28. break;
  29. }
  30. }
  31. }
  32. if ( isValid == true )
  33. return true;
  34. else
  35. return false;
  36. }
  37. // --------------------------------------------------------------------------------------
  38. // this method will validate only float values.
  39. function all_float(str) {
  40. var isValid = true;
  41. var validChars = "0123456789.";
  42. var charIndex;
  43. for (charIndex = 0; charIndex < str.length;charIndex++) {
  44. if ( validChars.indexOf( str.charAt(charIndex).toLowerCase() ) < 0 ) {
  45. isValid = false;
  46. break;
  47. }
  48. }
  49. if ( isValid == true )
  50. return true;
  51. else
  52. return false;
  53. }
  54. // --------------------------------------------------------------------------------------
  55. // this method will cvalidate alphanumeric and space.
  56. function alphanumeric(str) {
  57. var isValid = true;
  58. var validChars = "abcdefghijklmnopqrstuvwxyz0123456789. ";
  59. var charIndex;
  60. for (charIndex = 0; charIndex < str.length;charIndex++) {
  61. if ( validChars.indexOf( str.charAt(charIndex).toLowerCase() ) < 0 ) {
  62. isValid = false; break;
  63. }
  64. }
  65. if ( isValid == true ) return true; else return false;
  66. }
  67. // --------------------------------------------------------------------------------------
  68. // this method will validate the email address.
  69. function email_address(email) {
  70. var mailformat = /^\w+([\.-]?\w)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
  71. if ( email.match( mailformat ) ) return true; else return false;
  72. }
  73. // --------------------------------------------------------------------------------------
  74. // this method will check the area code of a phone number.
  75. function area_code(first_three) {
  76. var area_codes = [ "049", "058", "013", "042", "046", "054", "028", "011", "053", "027",
  77. "040", "051", "014", "031", "035", "017", "039", "015", "056", "023",
  78. "018", "021", "012", "022", "044", "032", "045", "036", "043", "028",
  79. "047", "034", "055", "033", "016", "052", "057" ];
  80.  
  81. for ( var i = 0; i < area_codes.length; i++ ) {
  82. if ( area_codes[i] == first_three ) { return true; break; }
  83. }
  84. return false;
  85. }
  86. // --------------------------------------------------------------------------------------
  87. // this method will check the area code of a cell number.
  88. function cell_code(first_two) {
  89. var cell_codes = [ "78", "76", "72", "71", "74", "73", "82", "83", "84", "81", "60", "61" ];
  90. for ( var i = 0; i < cell_codes.length; i++ ) {
  91. if ( cell_codes[i] == first_two ) { return true; break; }
  92. }
  93. return false;
  94. }
  95. // --------------------------------------------------------------------------------------
  96. function ValidateIDnumber(idnumber) {
  97. //1. numeric and 13 digits
  98. if ( isNaN(idnumber) || (idnumber.length != 13 ) ) {
  99. return false;
  100. }
  101.  
  102. //2. first 6 numbers is a valid date
  103. var tempDate = new Date(idnumber.substring(0, 2), idnumber.substring(2, 4) - 1, idnumber.substring(4, 6));
  104. var currentTime = new Date();
  105. var diff = currentTime - tempDate ;
  106. var age = Math.floor( ( diff / 1000 / (60 * 60 * 24) ) / 365.25 );
  107. if ( age < 18 ) {
  108. alert("You don't qualify to vote - you will vote after (" + ( 18 - age ) + ") year(s)") ;
  109. return false ;
  110. }
  111. if (!((tempDate.getYear() == idnumber.substring(0, 2))
  112. && (tempDate.getMonth() == idnumber.substring(2, 4) - 1)
  113. && (tempDate.getDate() == idnumber.substring(4, 6)))) {
  114. return false;
  115. }
  116.  
  117. //3. luhn formula
  118. var tempTotal = 0;
  119. var checkSum = 0;
  120. var multiplier = 1;
  121. for (var i = 0; i < 13; ++i) {
  122. tempTotal = parseInt(idnumber.charAt(i)) * multiplier;
  123. if (tempTotal > 9) {
  124. tempTotal = parseInt(tempTotal.toString().charAt(0)) + parseInt(tempTotal.toString().charAt(1));
  125. }
  126. checkSum = checkSum + tempTotal;
  127. multiplier = ( multiplier % 2 == 0 ) ? 1 : 2;
  128. }
  129. if ((checkSum % 10) == 0) { return true; } else { return false; }
  130. } function date_validation(date) { var reg = /^\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}$/ ; if ( !date.match( reg ) ) { return false ; } else { return true ; } }
  131. // --------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement