表单校验
checkIfBlur 当表单的input失去焦点时校验
showErrorTips 显示错误提示
hideErrorTips 隐藏错误提示
表单校验
Kind: global function
Param | Type | Description |
---|---|---|
selector | string |
表单的selector |
callback | function |
校验后的回调 |
[options] | Object |
配置项 |
[options.regexp] | object |
表单所需的正则表达式 |
Example
<input type="tel" required pattern="[0-9]{11}" placeholder="输入你现在的手机号" emptyTips="请输入手机号" notMatchTips="请输入正确的手机号">
<input type="text" required pattern="REG_IDNUM" placeholder="输入你的身份证号码" emptyTips="请输入身份证号码" notMatchTips="请输入正确的身份证号码">
pattern="REG_IDNUM"
,则需要在调用相应方法时传入{regexp:{IDNUM: /(?:^\d{15}$)|(?:^\d{18}$)|^\d{17}[\dXx]$/}}
,详情请看下面checkIfBlur
和validate
radio需要检验,只需把参数写在同一表单下,同name的第一个元素即可。
<input type="radio" value="male" name="sex" required tips="请选择性别" />
<input type="radio" value="female" name="sex" />
checkbox需要校验,只需把参数写在同一表单下,同name的第一个元素即可。 pattern 规定选择个数,用法与正则一致,例如:
<input type="checkbox" name="assistance" value="黄药师" required pattern="{1,2}" tips="请勾选1-2个敲码助手" />
<input type="checkbox" name="assistance" value="欧阳锋" />
<input type="checkbox" name="assistance" value="段智兴" />
<input type="checkbox" name="assistance" value="洪七公" />
// weui.form.validate('#form', function(error){ console.log(error);}); // error: {dom:[Object], msg:[String]}
weui.form.validate('#form', function (error) {
if (!error) {
const loading = weui.loading('提交中...');
setTimeout(function () {
loading.hide();
weui.toast('提交成功', 3000);
}, 1500);
}
// return true; // 当return true时,不会显示错误
}, {
regexp: {
IDNUM: /(?:^\d{15}$)|(?:^\d{18}$)|^\d{17}[\dXx]$/,
VCODE: /^.{4}$/
}
});
checkIfBlur 当表单的input失去焦点时校验
Kind: global function
Param | Type | Description |
---|---|---|
selector | string |
表单的selector |
[options] | Object |
配置项 |
[options.regexp] | object |
表单所需的正则表达式 |
Example
weui.form.checkIfBlur('#form', {
regexp: {
IDNUM: /(?:^\d{15}$)|(?:^\d{18}$)|^\d{17}[\dXx]$/,
VCODE: /^.{4}$/
}
});
showErrorTips 显示错误提示
Kind: global function
Param | Type | Description |
---|---|---|
error | Object |
错误数据 |
error.ele | string |
出错了的dom元素 |
error.msg | string |
出错了的msg。会根据此msg 找到对应的Tips (比如msg 是empty ),那么ele 上的emptyTips 就会以topTips 显示 |
Example
weui.form.showErrorTips({
ele: document.getElementById("xxxInput")
msg: 'empty'
});
hideErrorTips 隐藏错误提示
Kind: global function
Param | Type | Description |
---|---|---|
ele | Object |
dom元素 |
Example
weui.form.hideErrorTips(document.getElementById("xxxInput"));