uni-app进行表单效验

Uni-app内置了一些表单验证方法,可以帮助我们对表单进行有效的验证。以下是一些常用的验证方法:

  1. 非空验证:
if(!this.formData.name){
  uni.showToast({
    title: '请输入姓名',
    icon: 'none'
  });
  return false;
}

  1. 手机号码验证:
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;
}

  1. 邮箱验证:
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;
}

  1. 身份证号码验证:
const idCardReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
if(!idCardReg.test(this.formData.idCard)){
  uni.showToast({
    title: '请输入正确的身份证号码',
    icon: 'none'
  });
  return false;
}

可根据自己的需求进行组合验证,最后根据验证结果来判断表单是否可以提交。

你可能感兴趣的:(uni-app,前端,javascript)