## Functions
<dl>
<dt><a href="#validate">validate(selector, callback, [options])</a></dt>
<dd><p>表單校驗</p>
</dd>
<dt><a href="#checkIfBlur">checkIfBlur(selector, [options])</a></dt>
<dd><p>checkIfBlur 當表單的input失去焦點時校驗</p>
</dd>
<dt><a href="#showErrorTips">showErrorTips(error)</a></dt>
<dd><p>showErrorTips 顯示錯誤提示</p>
</dd>
<dt><a href="#hideErrorTips">hideErrorTips(ele)</a></dt>
<dd><p>hideErrorTips 隱藏錯誤提示</p>
</dd>
</dl>
<a name="validate"></a>
## validate(selector, callback, [options])
表單校驗
**Kind**: global function
| Param | Type | Description |
| --- | --- | --- |
| selector | <code>string</code> | 表單的selector |
| callback | <code>function</code> | 校驗后的回調 |
| [options] | <code>Object</code> | 配置項 |
| [options.regexp] | <code>object</code> | 表單所需的正則表達式 |
**Example**
##### 普通input的HTML
```html
<input type="tel" required pattern="[0-9]{11}" placeholder="輸入你現在的手機號" emptyTips="請輸入手機號" notMatchTips="請輸入正確的手機號">
<input type="text" required pattern="REG_IDNUM" placeholder="輸入你的身份證號碼" emptyTips="請輸入身份證號碼" notMatchTips="請輸入正確的身份證號碼">
```
- required 表示需要校驗
- pattern 表示校驗的正則,不填則進行為空校驗。當以REG_開頭時,則獲取校驗時傳入的正則。如`pattern="REG_IDNUM"`,則需要在調用相應方法時傳入`{regexp:{IDNUM: /(?:^\d{15}$)|(?:^\d{18}$)|^\d{17}[\dXx]$/}}`,詳情請看下面`checkIfBlur`和`validate`
- 報錯的wording會從 emptyTips | notMatchTips | tips | placeholder 里獲得
<br>
##### radio
radio需要檢驗,只需把參數寫在同一表單下,同name的第一個元素即可。
```html
<input type="radio" value="male" name="sex" required tips="請選擇性別" />
<input type="radio" value="female" name="sex" />
```
<br>
##### checkbox
checkbox需要校驗,只需把參數寫在同一表單下,同name的第一個元素即可。
pattern 規定選擇個數,用法與正則一致,例如:
```html
<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="洪七公" />
```
- {1,} 至少選擇1個
- {1,2} 選擇1-2個
- 這里不會出現{0,}這種情況,因為有required就表示必選。否則直接去掉required即可。
<br>
``` js
// weui.form.validate('#form', function(error){ console.log(error);}); // error: {dom:[Object], msg:[String]}
weui.form.validate('#form', function (error) {
if (!error) {
var 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}$/
}
});
```
<a name="checkIfBlur"></a>
## checkIfBlur(selector, [options])
checkIfBlur 當表單的input失去焦點時校驗
**Kind**: global function
| Param | Type | Description |
| --- | --- | --- |
| selector | <code>string</code> | 表單的selector |
| [options] | <code>Object</code> | 配置項 |
| [options.regexp] | <code>object</code> | 表單所需的正則表達式 |
**Example**
```js
weui.form.checkIfBlur('#form', {
regexp: {
IDNUM: /(?:^\d{15}$)|(?:^\d{18}$)|^\d{17}[\dXx]$/,
VCODE: /^.{4}$/
}
});
```
<a name="showErrorTips"></a>
## showErrorTips(error)
showErrorTips 顯示錯誤提示
**Kind**: global function
| Param | Type | Description |
| --- | --- | --- |
| error | <code>Object</code> | 錯誤數據 |
| error.ele | <code>string</code> | 出錯了的dom元素 |
| error.msg | <code>string</code> | 出錯了的msg。會根據此`msg`找到對應的`Tips`(比如`msg`是`empty`),那么`ele`上的`emptyTips`就會以`topTips`顯示 |
**Example**
```js
weui.form.showErrorTips({
ele: document.getElementById("xxxInput")
msg: 'empty'
});
```
<a name="hideErrorTips"></a>
## hideErrorTips(ele)
hideErrorTips 隱藏錯誤提示
**Kind**: global function
| Param | Type | Description |
| --- | --- | --- |
| ele | <code>Object</code> | dom元素 |
**Example**
```js
weui.form.hideErrorTips(document.getElementById("xxxInput"));
```