搜索
首页微信小程序小程序开发微信小程序)表单验证开发二

微信小程序)表单验证开发二

Mar 17, 2018 pm 01:38 PM
小程序开发验证

上一篇我们和大家分享了微信小程序之表单组件分享一,本文我们主要和大家分享微信小程序)表单验证开发二,希望能帮助到大家。

一.知识点

1.是否为空

if(e.detail.value.userName.length==0||e.detail.value.psw.length==0||e.detail.value.tel.length==0){
	      	this.setData({
	        	tip:'提示:不能为空!',
	      	})
    	}

2.正则表达式:

手机号码:var regtel=new RegExp('(^1[3|4|5|7|8][0-9]{9}$)','g');

26011905d76b353669dd49eb0731b7fe
 if(!regtel.exec(e.detail.value.tel)){
    		this.setData({
	        	tip:'手机号码格式不正确!',
	      	})
    	}

二.看列子

1.index.wxml

<form bindsubmit="formSubmit" bindreset="formReset">
	<view class="section">
		<view class="section__title">姓名:</view>
		<input name="userName" placeholder="请输入姓名"  maxlength="12" type="text" focus="false" class="section__iput"/>
	</view>
	<view class="section">
  		<view class="section__title">手机号码:</view>
  		<input  name="tel" placeholder="请输入手机号码"  type="text" focus="false" class="section__iput"/>
	</view>
	<view class="section">
  		<view class="section__title">密码:</view>
  		<input name="psw" placeholder="请输入您的密码" password="true" maxlength="12" type="text" focus="false" class="section__iput"/>
	</view>
	<view class="section section_gap">
		<view class="section__title">性别:</view>
		<radio-group name="radio-group">
			<label><radio value="radio1" checked="ture"/>男</label>
			<label><radio value="radio2"/>女</label>
		</radio-group>
	</view>
	<view class="section">
  		<view class="section__title">日期:</view>
	  	<picker mode="date" value="{{date}}" start="2015-09-01" end="2017-09-01" bindchange="bindDateChange" class="section__iput">
	    	<view class="picker">
	     	{{date}}
	    	</view>
	  	</picker>
	</view>
	<view class="section">
		<view class="section__title">留言:</view>
	  	<textarea auto-height placeholder="请输入内容" />
	</view>
	<view>{{tip}}</view>
	<view class="btn-area">
		<button formType="submit" type="primary">Submit</button>
		<button formType="reset"  type="default">Reset</button>
	</view>
</form>


2.index.wxss

.section{
	margin:10px 20px; 
	display:flex;
	border-bottom:1px solid #ccc;
	padding:15px 0;
}
.section__title{
	width:30%;
}
.section__iput{
	width:70%;
	line-height:25px;
	border:1px solid #ccc;
}
.btn-area{
	display:flex;
	justify-content:center;
	margin:20px;
}
.btn-area button{
	width:40%;
}

3.index.js

var app = getApp()
Page({
	data: {
	    date: '2016-09-01',
	    tip:'',
	},
	//日期
	bindDateChange: function(e) {
	    this.setData({
	      date: e.detail.value
	    })
	},
	//提交
	formSubmit: function(e) {
		var regtel=new RegExp('(^1[3|4|5|7|8][0-9]{9}$)','g');
		if(e.detail.value.userName.length==0||e.detail.value.psw.length==0||e.detail.value.tel.length==0){
	      	this.setData({
	        	tip:'提示:不能为空!',
	      	})
    	}else if(!regtel.exec(e.detail.value.tel)){
    		this.setData({
	        	tip:&#39;手机号码格式不正确!&#39;,
	      	})
    	}else{
    		this.setData({
	        	tip:'',
	      	})
    	}
	},
	//重置
	formReset: function() {
	    console.log('form发生了reset事件')
	}
})

有更好的方式,欢迎大家来补充。。。吐舌头

相关推荐:

微信小程序之表单组件分享一

html中关于表单组件的实例详解

关于HTML中表单组件的具体介绍

以上是微信小程序)表单验证开发二的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),