CheckValid.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import {Toast} from "@ant-design/react-native";
  2. const CheckValid = (value, type) => {
  3. if (type === 'ID') {
  4. const aCity = {
  5. 11: "北京",
  6. 12: "天津",
  7. 13: "河北",
  8. 14: "山西",
  9. 15: "内蒙古",
  10. 21: "辽宁",
  11. 22: "吉林",
  12. 23: "黑龙江",
  13. 31: "上海",
  14. 32: "江苏",
  15. 33: "浙江",
  16. 34: "安徽",
  17. 35: "福建",
  18. 36: "江西",
  19. 37: "山东",
  20. 41: "河南",
  21. 42: "湖北",
  22. 43: "湖南",
  23. 44: "广东",
  24. 45: "广西",
  25. 46: "海南",
  26. 50: "重庆",
  27. 51: "四川",
  28. 52: "贵州",
  29. 53: "云南",
  30. 54: "西藏",
  31. 61: "陕西",
  32. 62: "甘肃",
  33. 63: "青海",
  34. 64: "宁夏",
  35. 65: "新疆",
  36. 71: "台湾",
  37. 81: "香港",
  38. 82: "澳门",
  39. 91: "国外"
  40. }
  41. let iSum = 0;
  42. // 香港身份证
  43. if (/^[A-Z]{1,2}[0-9]{6}\(?[0-9A]\)?$/i.test(value))
  44. return true;
  45. // 澳门身份证
  46. if (/^[1|5|7][0-9]{6}\([0-9Aa]\)/i.test(value))
  47. return true;
  48. // 护照
  49. if (/^[A-Z]{1,2}[0-9]{8}$/i.test(value))
  50. return true;
  51. // 台湾身份证
  52. if (/^[a-zA-Z][0-9]{9}$/i.test(value))
  53. return true;
  54. if (!/^\d{17}(\d|x)$/i.test(value))
  55. return false;
  56. const sId = value.replace(/x$/i, "a");
  57. if (aCity[parseInt(sId.substr(0, 2))] == null)
  58. return false;
  59. //return "Error:非法地区";
  60. const sBirthday = sId.substr(6, 4) + "-" + Number(sId.substr(10, 2)) + "-" + Number(sId.substr(12, 2));
  61. var d = new Date(sBirthday.replace(/-/g, "/"));
  62. if (sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate()))
  63. return false;
  64. //return "Error:非法生日";
  65. for (var i = 17; i >= 0; i--)
  66. iSum += (Math.pow(2, i) % 11) * parseInt(sId.charAt(17 - i), 11)
  67. if (iSum % 11 !== 1)
  68. return false;
  69. //return "Error:非法证号";
  70. //alert(aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?"男":"女") );
  71. //return {province:aCity[parseInt(sId.substr(0,2))],
  72. // birthday:sBirthday,
  73. // gender:(sId.substr(16,1)%2?"男":"女") };
  74. return true;
  75. }
  76. else if (type === 'orgin') {
  77. if (/[^_IOZSVa-z\W]{2}\d{6}[^_IOZSVa-z\W]{10}/g.test(value))
  78. return true;
  79. return false;
  80. }
  81. else if (type === 'tel') {
  82. if (!value) {
  83. return false
  84. }
  85. if (!/^1[3456789]\d{9}$/.test(value) && !/^([0-9]{3,4}-)?[0-9]{7,8}$/.test(value)) {
  86. return false
  87. }
  88. return true
  89. }
  90. else if (type === 'num') {
  91. if (parseFloat(value) < 0 || isNaN(value)){
  92. return false
  93. }
  94. return true
  95. }
  96. return true
  97. }
  98. export default CheckValid;