百度/微信小程序用户输入手机号码格式限制(长度11位开头位为1)
2019-11-23 本文已影响0人
ForeverYoung_06
HTML部分
<!-- 遮罩层 -->
<view class="body" s-if="showmodelflag">
<view class="medium">
<view class="m-head">
<text> ·在线留言·</text>
<view class="shutdown" bindtap="closemodel"></view>
</view>
<view class="form-box">
<!-- <form bindsubmit="formSubmit"> -->
<view class="from-section">
<view class="sec-title">联系人</view>
<view class="ipt"><input bindinput="bindInputName" type="text" value="{{username}}" placeholder="请输入联系人" />
</view>
</view>
<view class="from-section">
<view class="sec-title">手机号</view>
<view class="ipt"><input bindinput="bindInputTele" type="number" value="{{usertele}}" placeholder="请输入手机号" />
</view>
</view>
<view class="from-section">
<view class="sec-title">公司名称</view>
<view class="ipt"><input bindinput="bindInputCom" type="text" value="{{company}}" placeholder="请输入公司名称" />
</view>
</view>
<view class="submit-box">
<button class="submit-btn" bindtap="submit">立即留言</button>
</view>
<!-- </form> -->
</view>
</view>
</view>
JS部分
//用户输入了用户名、手机号以及留言内容
if (this.data.username && this.data.usertele && this.data.company) {
if (this.data.usertele.length != 11) {//输入的手机号不足11位提示
return swan.showModal({
title: '提示',
content: '请输入11位手机号',
confirmText: '确定',
cancelText: '取消',
success(res) {
if (res.confirm) {
console.log("确定")
} else if (res.cancel) {
console.log("取消")
}
}
})
}
if (this.data.usertele.length == 11) {//输入的手机号满足11位
//正则匹配开头是1总长度为11的号码
let str = /^1\d{10}$/;
if (str.test(this.data.usertele)) {//满足条件发请求
var subData = {
data: JSON.stringify({ name: this.data.username, tel: this.data.usertele, user_id: this.data.shopid, supply_id: this.data.goodsid,content: this.data.company })
};
swan.request({
url: config.config.api_base_url + "shop_item/consult",
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
method: "POST",
data: subData,
success: res => {
// 信息正确且提交成功关闭模态框
this.setData({
showmodelflag: false,
username:'',
usertele:'',
company:'',
})
console.log("信息正确且提交成功")
swan.showToast({
title: '提交成功',
success() {
}
});
}
});
} else {
//手机号格式不正确
return swan.showModal({
title: '提示',
content: '手机号格式不正确',
confirmText: '确定',
cancelText: '取消',
success(res) {
if (res.confirm) {
console.log("确定")
} else if (res.cancel) {
console.log("取消")
}
}
})
}
}
}
每个女程序员,都必须拥有强壮的体魄。