詳解vue如何使用rules對錶單字段進行校驗

一、在代碼中,添加屬性::rulehtml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
< Form ref = "loginForm" :model = "form" :rules = "rules" @ keydown.enter.native = "handleSubmit" class = "form-con" >
            < FormItem prop = "phone" >
              < Input v-model = "form.phone" prefix = "md-person" placeholder = "請輸入您的手機號" size = "large" class = "login-form-input" :maxlength = "11" ></ Input >
            </ FormItem >
            < FormItem prop = "password" >
              < Input type = "password" v-model = "form.password" prefix = "md-lock" placeholder = "請輸入您的密碼" size = "large" class = "login-form-input" :maxlength = "100" ></ Input >
            </ FormItem >
            < FormItem prop = "code" >
              < Input v-model = "form.code" placeholder = "請輸入驗證碼" prefix = "ios-barcode" size = "large" class = "login-form-input" :maxlength = "4" ></ Input >
              < img @ click = "refLoginImg" :src = "loginImgSrc" class = "login-img" />
            </ FormItem >
            < FormItem >
              < Button @ click = "handleSubmit" :loading = "submitLoading" type = "info" long size = "large" class = "login-btn" icon = "md-log-in" >
                < span v-if = "!submitLoading" >當即登陸</ span >
                < span v-else>正在登陸,請稍候...</ span >
              </ Button >
            </ FormItem >
          </ Form >

2.新建一個文件(validate.js)定義驗證規則前端

?
1
2
3
4
5
6
7
8
9
10
11
12
13
/** 這個文件只容許放表單驗證方法 **/
 
//驗證手機號
export function isPhone(rule, value, callback) {
   if (!value) {
     return callback( new Error( '輸入不能夠爲空' ));
   }
   var pattern = /^1[34578]\d{9}$/
   if (pattern.test(value)){
     return callback()
   }
   return callback( new Error( '輸入的手機號錯誤' ))
}

三、在頁面(xx.vue)中引入驗證規則定義的文件,並在export default中定義rule規則,使用語法:{validator:驗證方法,trigger:驗證觸發} validatorvue

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
rules: {
           phone: [
             { required: true , message: '手機號不能爲空' , trigger: 'blur' },
             { type: 'string' , min: 11, message: '手機號不容許小於11位' , trigger: 'blur' },
             { validator: isPhone, trigger: 'blur' },
           ],
           password: [
             { required: true , message: '密碼不能爲空' , trigger: 'blur' },
             { type: 'string' , min: 6, message: '密碼不容許小於6位' , trigger: 'blur' },
           ],
           code: [
             { required: true , message: '驗證碼不能爲空' , trigger: 'blur' },
             { type: 'string' , min: 4, message: '驗證碼必須是4位' , trigger: 'blur' },
           ],
         },

如下是validator.js文件的部分驗證方法ios

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* 是不是公司郵箱*/
export function isWscnEmail(str) {
  const reg = /^[a-z0-9](?:[-_.+]?[a-z0-9]+)*@wallstreetcn\.com$/i;
  return reg.test(str.trim());
}
 
/* 合法uri*/
export function validateURL(textval) {
  const urlregex = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,? '\\+&%$#=~_-]+))*$/;
  return urlregex.test(textval);
}
 
// 驗證是否整數
export function isInteger(rule, value, callback) {
  if (!value) {
   return callback(new Error(' 輸入不能夠爲空 '));
  }
  setTimeout(() => {
   if (!Number(value)) {
    callback(new Error(' 請輸入正整數 '));
   } else {
    const re = /^[0-9]*[1-9][0-9]*$/;
    const rsCheck = re.test(value);
    if (!rsCheck) {
     callback(new Error(' 請輸入正整數 '));
    } else {
     callback();
    }
   }
  }, 1000);
}
 
 
// 驗證是不是[0-1]的小數
export function isDecimal(rule, value, callback) {
  if (!value) {
   return callback(new Error(' 輸入不能夠爲空 '));
  }
  setTimeout(() => {
   if (!Number(value)) {
    callback(new Error(' 請輸入數字 '));
   } else {
    if (value < 0 || value > 1) {
     callback(new Error(' 請輸入[0,1]之間的數字 '));
    } else {
     callback();
    }
   }
  }, 1000);
}
 
// 驗證端口是否在[0,65535]之間
export function isPort(rule, value, callback) {
  if (!value) {
   return callback(new Error(' 輸入不能夠爲空 '));
  }
  setTimeout(() => {
   if (value == ' ' || typeof(value) == undefined) {
    callback(new Error(' 請輸入端口值 '));
   } else {
    const re = /^([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/;
    const rsCheck = re.test(value);
    if (!rsCheck) {
     callback(new Error(' 請輸入在[0-65535]之間的端口值'));
    } else {
     callback();
    }
   }
  }, 1000);
}
 
/* 小寫字母*/
export function validateLowerCase(str) {
  const reg = /^[a-z]+$/;
  return reg.test(str);
}

 

 
後盾網VIP實戰教程之仿京東網系列視頻教程 共35講(含源碼) attach_img
捷訓網Node.js基礎入門培訓12篇 Node.js基礎入門學習視頻教程 attach_img
web前端框架bootstrap高清視頻教程附代碼講義 17課 attach_img
後盾網土特產商城前端實戰教程46講(含源碼)
企業門戶前端實戰(含源碼)教程 attach_img
前端開發項目:一塊兒來作秒殺活動 attach_img
web前端js漸進式顯示框架vue.js實戰高清視頻教程附代碼講義 11課 attach_img
Node.JS之「我的博客開發實戰教程」附代碼講義 26課 attach_img
Vue.js + Node.js實現音樂播放器實戰案例
The Vast World of Vue.js[英文版Vue.js教程]
英文版Learning Vuejs
vue.js實戰項目:Vue入門到實戰圖書管理
張容銘js高級課程vue.js實戰項目:團購網 attach_img
Vue2.0從入門到項目實戰:電商項目 attach_img
VueJs基礎入門詳解:45節課
最新Web界面庫vue.js視頻教程
vue.js實戰項目:美團外賣平臺  ...2
智能社vue.js精講視頻教程
麥子學院Vuejs詳解31課 attach_img
Bootstrap入門實戰網站後臺管理教程 attach_img
相關文章
相關標籤/搜索