小程序表单验证

首先准备第三方的WxValidate.js

https://github.com/skyvow/wx-extend/blob/master/docs/components/validate.md  下载地址


注意加载的时候要用到

用到index.js(自己定义的页面js)

import WxValidate from '../../../utils/WxValidate.js'
var Validate = ""


在onload 编辑代码

  1. onLoad: function (options) {
  2. // 页面初始化 options为页面跳转所带来的参数
  3. var that = this
  4. //数据进行校验
  5. const rules = {
  6. mobile: {
  7. required: true,
  8. tel: true,
  9. },
  10. consignee: {
  11. required: true,
  12. },
  13. address: {
  14. required: true,
  15. minlength: 2,
  16. },
  17. zipcode: {
  18. digits: true,
  19. }
  20. }
  21. // 验证字段的提示信息,若不传则调用默认的信息
  22. const messages = {
  23. mobile: {
  24. required: '请输入手机号码',
  25. tel: '请输入正确的手机号码',
  26. },
  27. consignee: {
  28. required: '请输入收货人',
  29. },
  30. address: {
  31. required: '请输入收货地址',
  32. minlength: '请输入正确的收货地址',
  33. },
  34. zipcode: {
  35. digits: '请输入正确的邮编'
  36. }
  37. }
  38. Validate = new WxValidate(rules,messages)
在formsubmit 使用


扫描二维码关注公众号,回复: 2493368 查看本文章
  1. const params = event.detail.value
  2. // 传入表单数据,调用验证方法
  3. if (!Validate.checkForm(event)) {
  4. const error = Validate.errorList[ 0]
  5. //提示信息
  6. return false
  7. }

猜你喜欢

转载自blog.csdn.net/liu709127859/article/details/81044110