validate.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. export function isPlainObject(obj) {
  2. return Object.prototype.toString.call(obj) === "[object Object]";
  3. }
  4. export function isString(str) {
  5. return typeof str == "string";
  6. }
  7. export function isToday(time) {
  8. return new Date().getTime() - time < 86400000;
  9. }
  10. export function isEmpty(obj) {
  11. if (!obj) return true;
  12. if (Array.isArray(obj) && obj.length == 0) return true;
  13. if (isPlainObject(obj) && Object.values(obj).length == 0) return true;
  14. return false;
  15. }
  16. export function isUrl(str) {
  17. const reg =
  18. "^((https|http|ftp|rtsp|mms)?://)" +
  19. "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" + //ftp的user@
  20. "(([0-9]{1,3}.){3}[0-9]{1,3}" + // IP形式的URL- 199.194.52.184
  21. "|" + // 允许IP和DOMAIN(域名)
  22. "([0-9a-z_!~*'()-]+.)*" + // 域名- www.
  23. "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]." + // 二级域名
  24. "[a-z]{2,6})" + // first level domain- .com or .museum
  25. "(:[0-9]{1,4})?" + // 端口- :80
  26. "((/?)|" + // 如果没有文件名,则不需要斜杠
  27. "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
  28. return new RegExp(reg).test(str) ? true : false;
  29. }
  30. export function isFunction(val) {
  31. return val && typeof val === "function";
  32. }
  33. export function isEng(val) {
  34. return /^[A-Za-z]+$/.test(val);
  35. }