Uni-app内置了一些表单验证方法,可以帮助我们对表单进行有效的验证。以下是一些常用的验证方法:
- if(!this.formData.name){
- uni.showToast({
- title: '请输入姓名',
- icon: 'none'
- });
- return false;
- }
- const phoneReg = /^1[3|4|5|6|7|8|9][0-9]{9}$/;
- if(!phoneReg.test(this.formData.phone)){
- uni.showToast({
- title: '请输入正确的手机号码',
- icon: 'none'
- });
- return false;
- }
- const emailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
- if(!emailReg.test(this.formData.email)){
- uni.showToast({
- title: '请输入正确的邮箱地址',
- icon: 'none'
- });
- return false;
- }
- const idCardReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
- if(!idCardReg.test(this.formData.idCard)){
- uni.showToast({
- title: '请输入正确的身份证号码',
- icon: 'none'
- });
- return false;
- }
可根据自己的需求进行组合验证,最后根据验证结果来判断表单是否可以提交。