微信小程序?qū)崿F(xiàn)表單驗(yàn)證提交功能
本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)表單驗(yàn)證提交的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:
說明:點(diǎn)擊一鍵預(yù)約提交表單時(shí)我們需要驗(yàn)證一些必填項(xiàng)以及它們的格式。微信小程序表單驗(yàn)證跟vue的input雙向綁定不同,微信小程序只能通過button按鈕提交form表單,然后通過監(jiān)聽表單提交方法去獲取提交的數(shù)據(jù)。
<!-- 表單 --> <view class="forms"> ? <view class="container"> ? ? <image class="bg" src="../../images/formBg.png" mode="aspectFill"></image> ? ? <view class="title"> ? ? ? <text class="text">我家裝修需要花多少錢?</text> ? ? ? <text class="text">免費(fèi)快速獲取預(yù)算方案</text> ? ? </view> ? ? <form class="" catchsubmit="submitFn"> ? ? ? <view class="item"> ? ? ? ? <text class="text">*</text> ? ? ? ? <picker class="" mode="region" bindchange="bindRegionChange" value="{{region}}"> ? ? ? ? ? <input type="text" name="city" value="{{city}}" placeholder="請(qǐng)選擇房屋所在城市" placeholder-class="input-placeholder" /> ? ? ? ? </picker> ? ? ? </view> ? ? ? <view class="item"> ? ? ? ? <text class="text"></text> ? ? ? ? <input type="text" name="area" value="{{area}}" class="weui-input" placeholder="請(qǐng)輸入房屋面積" placeholder-class="input-placeholder" /> ? ? ? </view> ? ? ? <view class="item"> ? ? ? ? <text class="text"></text> ? ? ? ? <input type="text" name="name" value="{{name}}" class="weui-input" placeholder="請(qǐng)輸入您的姓名" placeholder-class="input-placeholder" /> ? ? ? </view> ? ? ? <view class="item"> ? ? ? ? <text class="text">*</text> ? ? ? ? <input type="text" name="phone" value="{{phone}}" class="weui-input" placeholder="請(qǐng)輸入聯(lián)系電話" placeholder-class="input-placeholder" /> ? ? ? </view> ? ? ? <button class="btn" formType="submit"> ? ? ? ? <text>一鍵預(yù)約</text> ? ? ? ? <!-- <image class="img" src="../../images/headglobal.png" mode="aspectFill"></image> --> ? ? ? </button> ? ? ? <view class="desc">裝企提供免費(fèi)上門量房服務(wù)、出3套方案供您對(duì)比</view> ? ? </form> ? </view> </view>
.forms { ? ? padding: 0 30rpx; ? ? .container { ? ? ? ? position: relative; ? ? ? ? width: 100%; ? ? ? ? padding: 20rpx; ? ? } ? ? .bg { ? ? ? ? position: absolute; ? ? ? ? left: 0; ? ? ? ? right: 0; ? ? ? ? top: 0; ? ? ? ? bottom: 0; ? ? ? ? width: 100%; ? ? ? ? height: 100%; ? ? ? ? z-index: -1; ? ? } ? ? .title { ? ? ? ? text-align: center; ? ? ? ? margin-bottom: 40rpx; ? ? ? ? .text { ? ? ? ? ? ? display: block; ? ? ? ? ? ? font-size: 48rpx; ? ? ? ? ? ? color: #000; ? ? ? ? } ? ? } ? ? .item { ? ? ? ? height: 65rpx; ? ? ? ? background-color: #fff; ? ? ? ? border: solid 1rpx #dddddd; ? ? ? ? border-radius: 10rpx; ? ? ? ? padding: 0 10rpx; ? ? ? ? margin-bottom: 20rpx; ? ? ? ? display: flex; ? ? ? ? align-items: center; ? ? ? ? .text { ? ? ? ? ? ? color: #ff0000; ? ? ? ? ? ? display: inline-block; ? ? ? ? ? ? width: 30rpx; ? ? ? ? ? ? font-size: 24rpx; ? ? ? ? } ? ? ? ? .weui-input { ? ? ? ? ? ? font-size: 28rpx; ? ? ? ? } ? ? ? ? .input-placeholder { ? ? ? ? ? ? color: #999; ? ? ? ? } ? ? } ? ? .btn { ? ? ? ? width: 100%; ? ? ? ? height: 75rpx; ? ? ? ? background-color: #00a0e9; ? ? ? ? box-shadow: 3rpx 4prx 13rpx 0rpx rgba(93, 200, 249, 0.59); ? ? ? ? border-radius: 6rpx; ? ? ? ? text-align: center; ? ? ? ? line-height: 75rpx; ? ? ? ? margin: 30rpx 0; ? ? ? ? position: relative; ? ? ? ? text { ? ? ? ? ? ? color: #fff; ? ? ? ? } ? ? } ? ? .desc { ? ? ? ? text-align: center; ? ? ? ? color: #999; ? ? ? ? font-size: 26rpx; ? ? }? ? .img { ? ? ? ? position: absolute; ? ? ? ? width: 31rpx; ? ? ? ? height: 47rpx; ? ? ? ? right: 80rpx; ? ? ? ? top: 40rpx; ? ? } }
data:{ ?? ?city:'', ? ? area: "", ? ? name: "", ? ? phone: "", ? ? region: ["廣東省", "廣州市", "海珠區(qū)"], }, // 表單提交 submitFn: function (e) { ? console.log(e); ? let that = this; ? if (e.detail.value.city == "") { ? ? wx.showToast({ ? ? ? title: "請(qǐng)選擇房屋所在城市", ? ? ? icon: "none", ? ? }); ? ? return false; ? } ? if (e.detail.value.phone == "") { ? ? wx.showToast({ ? ? ? title: "請(qǐng)輸入聯(lián)系電話", ? ? ? icon: "none", ? ? }); ? ? return false; ? } ? // 驗(yàn)證電話格式 ? if (!/^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/.test(e.detail.value.phone)) { ? ? wx.showToast({ ? ? ? title: "手機(jī)號(hào)碼有誤", ? ? ? duration: 2000, ? ? ? icon: "none", ? ? }); ? ? return false; ? } }, // 選擇城市 bindRegionChange: function (e) { ? console.log("picker發(fā)送選擇改變,攜帶值為", e.detail.value); ? this.setData({ ? ? city: e.detail.value, ? }); },
官網(wǎng):表單組件form
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript setInterval()與setTimeout()計(jì)時(shí)器
這篇文章主要介紹了JavaScript setInterval()與setTimeout()計(jì)時(shí)器,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12JS中注入eval, Function等系統(tǒng)函數(shù)截獲動(dòng)態(tài)代碼
這篇文章主要介紹了JS中注入eval, Function等系統(tǒng)函數(shù)截獲動(dòng)態(tài)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04JavaScript實(shí)現(xiàn)跟隨滾動(dòng)緩沖運(yùn)動(dòng)廣告框
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)跟隨滾動(dòng)緩沖運(yùn)動(dòng)廣告框,頁面左右兩邊跟隨式廣告框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07alixixi runcode.asp的代碼不錯(cuò)的應(yīng)用
alixixi runcode.asp的代碼不錯(cuò)的應(yīng)用...2007-08-08postman自定義函數(shù)實(shí)現(xiàn) 時(shí)間函數(shù)的思路詳解
Postman是一款功能強(qiáng)大的網(wǎng)頁調(diào)試與發(fā)送網(wǎng)頁HTTP請(qǐng)求的Chrome插件。這篇文章主要給大家介紹postman自定義函數(shù)實(shí)現(xiàn) 時(shí)間函數(shù)的思路詳解,感興趣的朋友一起看看吧2019-04-04使用JavaScript刪除HTML元素的2種方法及3種情況
給定一個(gè)HTML元素,如何使用JavaScript從文檔中刪除該HTML元素,這篇文章主要給大家介紹了關(guān)于使用JavaScript刪除HTML元素的2種方法及3種情況,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01JavaScript實(shí)現(xiàn)時(shí)間范圍效果
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)時(shí)間范圍效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05Javascript中call和apply函數(shù)的比較和使用實(shí)例
這篇文章主要介紹了Javascript中call和apply函數(shù)的比較和使用實(shí)例,本文試圖用更加清晰簡單的思路來分析解釋這兩個(gè)函數(shù),需要的朋友可以參考下2015-02-02