MCPcopy Create free account
hub / github.com/circircir-circle/human_exist_device / __initMethods

Method __initMethods

mini_program/common/Validate.js:64–288  ·  view source on GitHub ↗

* 初始化默认验证方法

()

Source from the content-addressed store, hash-verified

62 * 初始化默认验证方法
63 */
64 __initMethods() {
65 const that = this
66 that.methods = {
67 /**
68 * 验证必填元素
69 */
70 required: {
71 valid(value, param) {
72 if (!that.depend(param)) {
73 return 'dependency-mismatch'
74 } else if (typeof value === 'number') {
75 value = value.toString()
76 } else if (typeof value === 'boolean') {
77 return !0
78 }
79 return value.length > 0
80 },
81 message(param) {
82 return param.message || '这是必填字段。';
83 }
84 },
85 /**
86 * 验证电子邮箱格式
87 */
88 email: {
89 valid(value, param) {
90 return that.optional(value) ||
91 /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
92 .test(value)
93 },
94 message(param) {
95 return param.message || '请输入有效的电子邮件地址。';
96 }
97 },
98 /**
99 * 验证手机格式
100 */
101 tel: {
102 valid(value, param) {
103 return that.optional(value) || /^1[34578]\d{9}$/.test(value)
104 },
105 message(param) {
106 return param.message || '请输入11位的手机号码。';
107 }
108
109 },
110 /**
111 * 验证URL格式
112 */
113 url: {
114 valid(value, param) {
115 return that.optional(value) ||
116 /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})).?)(?::\d{2,5})?(?:[/?#]\S*)?$/i
117 .test(value)
118 },
119 message(param) {
120 return param.message || '请输入有效的网址。';
121 }

Callers 1

__initMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected