Skip to content

Instantly share code, notes, and snippets.

@llccing
Last active January 27, 2021 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save llccing/10057c33890ab33494a83a9e2f06aee6 to your computer and use it in GitHub Desktop.
Save llccing/10057c33890ab33494a83a9e2f06aee6 to your computer and use it in GitHub Desktop.

常用正则表达式

逗号分隔的邮箱验证

const multiEmailRegex = /^((([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6}\,))*(([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})))$/;
const validateMultiEmails = (rule, value, callback) => {
  if (!value) {
    return callback(new Error('邮箱必填!'));
  } else if (!multiEmailRegex.test(value)) {
    return callback(new Error('格式不正确,若多个以逗号分隔'));
  }
  callback();
};

同时验证手机号或者电话号码

const validatePhone = (rule, value, callback) => {
  const phone = /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/;
  // /^1[3456789]\d{9}$/ 34578 不够用,可以增加一些
  const telePhone = /^1(3|4|5|7|8)\d{9}$/;
  if (!value) {
    return callback(new Error('联系电话或手机号必填!'));
  } else if (phone.test(value) || telePhone.test(value)) {
    callback();
  } else {
    return callback(new Error('请输入正确格式的联系电话或者手机号!'));
  }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment