Newer
Older
DH_Apicture / src / utils / validate.js
@zhangqy zhangqy on 29 Nov 2 KB first commit
  1. /**
  2. * 判断url是否是http或https
  3. * @param {string} path
  4. * @returns {Boolean}
  5. */
  6. export function isHttp(url) {
  7. return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1;
  8. }
  9.  
  10. /**
  11. * 判断path是否为外链
  12. * @param {string} path
  13. * @returns {Boolean}
  14. */
  15. export function isExternal(path) {
  16. return /^(https?:|mailto:|tel:)/.test(path);
  17. }
  18.  
  19. /**
  20. * @param {string} str
  21. * @returns {Boolean}
  22. */
  23. export function validUsername(str) {
  24. const valid_map = ['admin', 'editor'];
  25. return valid_map.indexOf(str.trim()) >= 0;
  26. }
  27.  
  28. /**
  29. * @param {string} url
  30. * @returns {Boolean}
  31. */
  32. export function validURL(url) {
  33. const reg =
  34. /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/;
  35. return reg.test(url);
  36. }
  37.  
  38. /**
  39. * @param {string} str
  40. * @returns {Boolean}
  41. */
  42. export function validLowerCase(str) {
  43. const reg = /^[a-z]+$/;
  44. return reg.test(str);
  45. }
  46.  
  47. /**
  48. * @param {string} str
  49. * @returns {Boolean}
  50. */
  51. export function validUpperCase(str) {
  52. const reg = /^[A-Z]+$/;
  53. return reg.test(str);
  54. }
  55.  
  56. /**
  57. * @param {string} str
  58. * @returns {Boolean}
  59. */
  60. export function validAlphabets(str) {
  61. const reg = /^[A-Za-z]+$/;
  62. return reg.test(str);
  63. }
  64.  
  65. /**
  66. * @param {string} email
  67. * @returns {Boolean}
  68. */
  69. export function validEmail(email) {
  70. const reg =
  71. /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  72. return reg.test(email);
  73. }
  74.  
  75. /**
  76. * @param {string} email
  77. * @returns {Boolean}
  78. */
  79. export function validPhone(phone) {
  80. const reg = /^1[0-9]{10}$/;
  81. return reg.test(phone);
  82. }
  83.  
  84. /**
  85. * @param {string} str
  86. * @returns {Boolean}
  87. */
  88. export function isString(str) {
  89. if (typeof str === 'string' || str instanceof String) {
  90. return true;
  91. }
  92. return false;
  93. }
  94.  
  95. /**
  96. * @param {Array} arg
  97. * @returns {Boolean}
  98. */
  99. export function isArray(arg) {
  100. if (typeof Array.isArray === 'undefined') {
  101. return Object.prototype.toString.call(arg) === '[object Array]';
  102. }
  103. return Array.isArray(arg);
  104. }