validate.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * 判断url是否是http或https
  3. * @returns {Boolean}
  4. * @param url
  5. */
  6. export const isHttp = (url: string): boolean => {
  7. return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1;
  8. };
  9. /**
  10. * 判断path是否为外链
  11. * @param {string} path
  12. * @returns {Boolean}
  13. */
  14. export const isExternal = (path: string) => {
  15. return /^(https?:|mailto:|tel:)/.test(path);
  16. };
  17. /**
  18. * @param {string} str
  19. * @returns {Boolean}
  20. */
  21. export const validUsername = (str: string) => {
  22. const valid_map = ['admin', 'editor'];
  23. return valid_map.indexOf(str.trim()) >= 0;
  24. };
  25. /**
  26. * @param {string} url
  27. * @returns {Boolean}
  28. */
  29. export const validURL = (url: string) => {
  30. const reg =
  31. /^(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.,?'\\+&%$#=~_-]+))*$/;
  32. return reg.test(url);
  33. };
  34. /**
  35. * @param {string} str
  36. * @returns {Boolean}
  37. */
  38. export const validLowerCase = (str: string) => {
  39. const reg = /^[a-z]+$/;
  40. return reg.test(str);
  41. };
  42. /**
  43. * @param {string} str
  44. * @returns {Boolean}
  45. */
  46. export const validUpperCase = (str: string) => {
  47. const reg = /^[A-Z]+$/;
  48. return reg.test(str);
  49. };
  50. /**
  51. * @param {string} str
  52. * @returns {Boolean}
  53. */
  54. export const validAlphabets = (str: string) => {
  55. const reg = /^[A-Za-z]+$/;
  56. return reg.test(str);
  57. };
  58. /**
  59. * @param {string} email
  60. * @returns {Boolean}
  61. */
  62. export const validEmail = (email: string) => {
  63. const reg =
  64. /^(([^<>()\]\\.,;:\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,}))$/;
  65. return reg.test(email);
  66. };
  67. /**
  68. * @param {string} str
  69. * @returns {Boolean}
  70. */
  71. export const isString = (str: any) => {
  72. return typeof str === 'string' || str instanceof String;
  73. };
  74. /**
  75. * @param {Array} arg
  76. * @returns {Boolean}
  77. */
  78. export const isArray = (arg: string | string[]) => {
  79. if (typeof Array.isArray === 'undefined') {
  80. return Object.prototype.toString.call(arg) === '[object Array]';
  81. }
  82. return Array.isArray(arg);
  83. };