guhongwei 4 years ago
parent
commit
f63fd7885e
2 changed files with 33 additions and 8 deletions
  1. 31 6
      pages/login/login.js
  2. 2 2
      pages/login/login.wxml

+ 31 - 6
pages/login/login.js

@@ -1,17 +1,42 @@
 //index.js
+import WxValidate from '../../utils/wxValidate';
 //获取应用实例
 const app = getApp()
 
 Page({
   data: {
+    form: {}
+  },
+  initValidate() {
+    const rules = {
+      phone: {
+        required: true,
+        tel: true
+      },
+      password: { required: true, },
+    }
+    // 验证字段的提示信息,若不传则调用默认的信息
+    const messages = {
+      phone: { required: '请输入手机号', },
+      password: { required: '请输入密码', },
+    };
+    this.WxValidate = new WxValidate(rules, messages)
   },
   formSubmit: function (e) {
-    let form = e.detail.value;
-    console.log(form);
-    wx.redirectTo({
-      url: '/pages/home/home'
-    })
+    const params = e.detail.value;
+    if (!this.WxValidate.checkForm(params)) {
+      const error = this.WxValidate.errorList[0];
+      wx.showToast({
+        title: error.msg,
+        icon: 'none',
+        duration: 2000
+      })
+      return false
+    } else {
+      console.log(params);
+    }
   },
-  onLoad: function () {
+  onLoad: function (options) {
+    this.initValidate()//验证规则函数
   },
 })

+ 2 - 2
pages/login/login.wxml

@@ -8,10 +8,10 @@
     <form bindsubmit="formSubmit" bindreset="formReset">
       <view class='content'>
         <view class='input'>
-          <input type='number' name="phone" placeholder='请输入您本人的手机号码'></input>
+          <input type='number' name="phone" value="{{form.phone}}" maxlength="11" placeholder='请输入您本人的手机号码'></input>
         </view>
         <view class='input'>
-          <input type='text' name="password" placeholder='请输入密码' password></input>
+          <input type='text' name="password" value="{{form.password}}" placeholder='请输入密码' password></input>
         </view>
       </view>
       <view class="save">