validate.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * @description 是否外链
  3. * @param {String} path
  4. * @returns {Boolean}
  5. */
  6. export function isExternal(path) {
  7. return /^(https?:|mailto:|tel:)/.test(path);
  8. }
  9. /**
  10. * @description 只允许输入两位小数
  11. * @param {String} value
  12. * @returns { Number }
  13. */
  14. export function twoDecimal(value) {
  15. if (!value) {
  16. return '';
  17. }
  18. let val = value;
  19. val = val.replace(/[^\d.]/g, ''); // 清除"数字"和"."以外的字符
  20. val = val.replace(/\.{2,}/g, '.'); // 只保留第一个 "." 清除多余的
  21. val = val.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.');
  22. val = val.replace(/^(-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); // 只能输入两位小数
  23. return val;
  24. }
  25. /**
  26. * @description 是否是数字
  27. * @param {String|Number} value
  28. * @returns {Boolean}
  29. */
  30. export function isNumber(value) {
  31. let _val = Number(value);
  32. if (typeof _val === 'number') {
  33. // 检查是否是有限数并且不是NaN
  34. return isFinite(_val) && !isNaN(_val);
  35. }
  36. return false;
  37. }
  38. /**
  39. * 判断 Node 元素类型
  40. * @param {Node} node
  41. * @param {String} type
  42. */
  43. export function isNodeType(node, type) {
  44. return node.nodeName.toLowerCase() === type.toLowerCase();
  45. }