| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- // 防抖 只有在用户停止触发事件一段时间后才执行事件处理函数
- export function debounce(fn, delay) {
- let timer = null;
- return function (...args) {
- if (timer) clearTimeout(timer);
- timer = setTimeout(() => {
- fn.apply(this, args);
- }, delay);
- };
- }
- // 节流 确保在一段时间内只执行一次事件处理函数
- export function throttle(fn, delay) {
- let lastTime = 0; // 记录上一次执行的时间
- return function (...args) {
- const now = Date.now();
- if (now - lastTime >= delay) {
- lastTime = now;
- fn.apply(this, args);
- }
- };
- }
- /**
- * 验证手机号格式 以13/15/17/18/19开头
- * @param {Number} phone 手机号码
- */
- export function isValidPhone(phone) {
- const regex = /^1[35789]\d{9}$/;
- return regex.test(phone.trim());
- }
- /**
- * 验证密码格式:6-16位数字+字母组合,需包含至少1位字母
- * @param {String} password 密码
- */
- export function isValidPassword(password) {
- const pwd = password.trim();
- // 检查长度
- if (pwd.length < 6 || pwd.length > 16) {
- uni.$u.toast('密码长度应大于6小于16');
- return false;
- }
- // 检查是否只包含字母和数字
- if (!/^[a-zA-Z0-9]+$/.test(pwd)) {
- uni.$u.toast('密码只能使用字母和数字,不能使用其他字符');
- return false;
- }
- // 检查是否包含至少1个字母
- if (!/[a-zA-Z]/.test(pwd)) {
- uni.$u.toast('密码中至少包含1个字母');
- return false;
- }
- return true;
- }
|