1. /**
    2. * Created by outside on 2017/04/28
    3. *
    4. * @todo 提供统一的正则表达式规则和校验状态
    5. */
    6. const REGEX = {
    7. // 数字正则 用于全数字的校验
    8. number: /^(\d+)$/,
    9. // 正整数正则 用于身高等的校验
    10. integer: /^[0-9]+$/,
    11. // 中文的正则 用于中文名称等的校验
    12. chinese: /^[\u4e00-\u9fa5]+$/,
    13. // 中文和空格的正则 用于中文名称等的校验
    14. chineseSpace: /^[\u4e00-\u9fa5\u4dae\u0020\u36c3·]+$/,
    15. // 电话号码正则 用于电话号码的校验
    16. phone: /^1[3456789]\d{9}$/,
    17. // 年龄正则 用于对年龄1~199年龄段的校验
    18. age: /^[1-9][0-9]{0,2}$/,
    19. // 身份证正则 用于身份证的校验
    20. ID: /^\d{15}$|^\d{18}$|^\d{17}(\d|X|x)$/,
    21. // 金钱正则 用于都金钱各式的校验 (0 0.01 1.00 正确) (0. 05 0.001 错误)
    22. money: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
    23. // 月份正则 用于月份的校验 1 01 11
    24. month: /(^0?[1-9]$)|(^1[0-2]$)/,
    25. // 去除空格
    26. removeSpaces: /\s+/
    27. };
    28. const STATE = {
    29. // 是否为全数字
    30. isSpaces (str) {
    31. return REGEX.removeSpaces.test(str);
    32. },
    33. // 是否为全数字
    34. isNumber (str) {
    35. return REGEX.number.test(str);
    36. },
    37. // 是否为纯正整数
    38. isInteger (str) {
    39. return REGEX.integer.test(str);
    40. },
    41. // 是否为纯中文
    42. isChinese (str) {
    43. return REGEX.chinese.test(str);
    44. },
    45. // 是否是中文和空格
    46. isChineseSpace (str) {
    47. return REGEX.chineseSpace.test(str);
    48. },
    49. // 是否为手机号码
    50. isPhone (str) {
    51. return REGEX.phone.test(str);
    52. },
    53. // 是否为年龄格式
    54. isAge (str) {
    55. return REGEX.age.test(str);
    56. },
    57. // 是否为身份证号
    58. isID (str) {
    59. return REGEX.ID.test(str);
    60. },
    61. // 是否为金钱各式
    62. isMoney (str) {
    63. return REGEX.money.test(str);
    64. },
    65. // 是否为月份各式
    66. isMonth (str) {
    67. return REGEX.month.test(str);
    68. }
    69. }
    70. export default Object.assign(REGEX, STATE);