您现在的位置是:网站首页> 编程资料编程资料

微信小程序实现表单验证源码_javascript技巧_

2023-05-24 293人已围观

简介 微信小程序实现表单验证源码_javascript技巧_

本文实例为大家分享了微信小程序实现表单验证的具体代码,供大家参考,具体内容如下

效果图

点击预约设计弹出表单

城市,面积不能输入,只能选择

点击位置获取当前定位

源码:

Wxml

               {{designerDetail.name}}     {{designerDetail.honour}}                     设计费用:         {{designerDetail.attr_3}}                                    简介                -作品:{{designerDetail.attr_2}}         -擅长:{{designerDetail.attr_1}}         -毕业院校:{{designerDetail.attr_4}}         -从业年限:{{designerDetail.attr_5}}         -设计理念:{{designerDetail.attr_6}}                    案例作品                                          预约设计     预约量房          

js

// pages/designerList/designerDetail.js //获取应用实例 const app = getApp() Page({     /**    * 页面的初始数据    */   data: {     urlhttp: app.d.hostImg,//图片路径参数     designerDetail: {},     userlocation: '',//工程位置     acreagelist: ["60㎡以下", "60㎡-70㎡", "70㎡-100㎡", "100㎡以上"],//面积数组     userZoneList:[],//城市列表     price:0,     dialogHidden:true,     orderType:'',//预约类型     index: '',//面积下标     index2: '',//城市下标     btnStatus:true,     designerid:'',//设计师id   },     /**    * 生命周期函数--监听页面加载    */   onLoad: function (options) {     var that = this;     var id = options.id;     that.setData({       designerid:id,     })     console.log(id)     wx.showNavigationBarLoading();     wx.request({       url: app.d.ceshiUrl + 'home.designerDetail',         data: {         "id": id,       },       success: function (res) {         console.log(res)         var res = res.data;         that.setData({           designerDetail: res,         })         wx.hideNavigationBarLoading();       }     })          wx.request({       url: app.d.ceshiUrl + 'home.getService',       success: function (res) {         console.log(res)         var res = res.data;         that.setData({           userZoneList: res,         })          wx.hideNavigationBarLoading();       }     })       //支付金额     wx.request({       url: app.d.ceshiUrl + 'home.calc',       success: function (res) {         console.log(res)         var res = res.data;         that.setData({           price: res.price,         })       }     })   },   //选择所在区域   getLocation: function (e) {     var that = this;     wx.chooseLocation({       type: 'gcj02', //返回可以用于wx.openLocation的经纬度       success: function (res) {         var latitude = res.latitude         var longitude = res.longitude         console.log(res)         that.setData({           userlocation: res.name,           latitude: latitude,           longitude: longitude,         })       }     })   },   //所在城市选择   bindPickerzone: function (e) {     console.log('picker发送选择改变,携带值为', e.detail.value)     this.setData({       index2: e.detail.value     })   },   //房屋面积选择   bindPickerChange: function (e) {     console.log('picker发送选择改变,携带值为', e.detail.value)     this.setData({       index: e.detail.value     })   },   //图片预览   bindpreviewImage: function (e){     var that = this;     console.log(e.currentTarget.id)     var index = e.currentTarget.id;     var picslist = that.data.designerDetail.pics;     for (var i in picslist) {       picslist[i] = that.data.urlhttp + picslist[i];     }     wx.previewImage({       current: picslist[index], // 当前显示图片的http链接       urls: picslist // 需要预览的图片http链接列表     })   },   orderTap: function (e) {     var that = this;     console.log(e)     that.setData({       orderType: e.target.dataset.type,       dialogHidden: false,     })   },   closeTap: function () {     var that = this;     that.setData({       dialogHidden: true,     })   },   formSubmit: function (e) {     var that = this;     console.log(e.detail);     var value = e.detail.value;     var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;//电话校验     var acreage = that.data.acreagelist[that.data.index];     var userZone = that.data.userZoneList[that.data.index2];     var userlocation = that.data.userlocation;     that.setData({       btnStatus: false,     })     //所在城市     if (userZone == "" || userZone == null) {       wx.showModal({         title: "",         content: '请选择所在城市',         showCancel: false,         success: function (res) {           if (res.confirm) {             console.log('用户点击确定')           }         }       })       that.setData({         btnStatus: true,       })       return false;     }     //工程位置     if (userlocation == "" || userlocation == null) {       wx.showModal({         title: "",         content: '请选择工程位置',         showCancel: false,         success: function (res) {           if (res.confirm) {             console.log('用户点击确定')           }         }       })       that.setData({         btnStatus: true,       })       return false;     }     //房屋面积     if (acreage == "" || acreage == null) {       wx.showModal({         title: "",         content: '请选择您的房屋面积',         showCancel: false,         success: function (res) {           if (res.confirm) {             console.log('用户点击确定')           }         }       })       that.setData({         btnStatus: true,       })       return false;     }     //姓名     if (value.name == "" || value.name == null) {       wx.showModal({         title: "",         content: '姓名不能为空,请输入姓名',         showCancel: false,         success: function (res) {           if (res.confirm) {     
                
                

-六神源码网