找回密码
 立即注册

[uni-app] uni-app注册页面编写代码

[复制链接]
admin 发表于 前天 18:34 | 显示全部楼层 |阅读模式
uni-app注册页面编写代码
  1. <template>
  2.         <view>
  3.                 <uni-notice-bar show-icon scrollable :text="notice" />
  4.         </view>
  5.         <view class="reg-view">
  6.                 <uni-forms ref="userFormRef" :model="userForm" :label-width="80" :rules="rules">
  7.                         <uni-forms-item label="昵称" required name="nickname">
  8.                                 <uni-easyinput v-model="userForm.nickname" placeholder="请输入昵称" />
  9.                         </uni-forms-item>
  10.                         <uni-forms-item label="用户名" required name="username">
  11.                                 <uni-easyinput v-model="userForm.username" placeholder="请输入用户名" />
  12.                         </uni-forms-item>
  13.                         <uni-forms-item label="密码" required name="password">
  14.                                 <uni-easyinput type="password" v-model="userForm.password" placeholder="请输入密码" />
  15.                         </uni-forms-item>
  16.                         <uni-forms-item label="确认密码" required name="password2">
  17.                                 <uni-easyinput type="password" v-model="userForm.password2" placeholder="请再次确认密码" />
  18.                         </uni-forms-item>
  19.                         <uni-forms-item label="性别" required name="sex">
  20.                                 <uni-data-checkbox v-model="userForm.sex" :localdata="sexs" />
  21.                         </uni-forms-item>
  22.                         <uni-forms-item label="手机号码" required name="phone">
  23.                                 <uni-easyinput v-model="userForm.phone" placeholder="请输入手机号码" />
  24.                         </uni-forms-item>
  25.                         <uni-forms-item label="验证码" required name="captcha">
  26.                                 <view style="display: flex;justify-content: space-between;">
  27.                                         <view class="" style="width: 50%;">
  28.                                                 <uni-easyinput v-model="userForm.captcha" placeholder="请输入验证码" />
  29.                                         </view>
  30.                                         <view class="" style="width: 48%;">
  31.                                                 <button type="default" style="width: 99%;height: 35px;line-height: 35px;"
  32.                                                         size="mini">获取验证码</button>
  33.                                         </view>
  34.                                 </view>
  35.                         </uni-forms-item>
  36.                 </uni-forms>
  37.                 <button type="primary" class="round-button" @tap="reg">注册</button>
  38.         </view>
  39. </template>
  40. <script setup>
  41.         import {
  42.                 ref
  43.         } from 'vue';
  44.         const userFormRef = ref()
  45.         const reg = () => {
  46.                 userFormRef.value.validate().then(res => {
  47.                         console.log('验证通过:', res);
  48.                 }).catch(err => {
  49.                         console.log('表单错误信息:', err);
  50.                 })
  51.         }
  52.         const userForm = ref({})
  53.         const sexs = ref([{
  54.                 text: '男',
  55.                 value: 0
  56.         }, {
  57.                 text: '女',
  58.                 value: 1
  59.         }, {
  60.                 text: '保密',
  61.                 value: 2
  62.         }])
  63.         const notice = ref("明天放假了")
  64.         // 表单验证规则
  65.         const rules = {
  66.                 // 对name字段进行必填验证
  67.                 nickname: {
  68.                         rules: [{
  69.                                         required: true,
  70.                                         errorMessage: '请输入昵称',
  71.                                 },
  72.                                 {
  73.                                         minLength: 2,
  74.                                         maxLength: 50,
  75.                                         errorMessage: '昵称长度在 {minLength} 到 {maxLength} 个字符',
  76.                                 }
  77.                         ]
  78.                 },
  79.                 username: {
  80.                         rules: [{
  81.                                         required: true,
  82.                                         errorMessage: '请输入用户名',
  83.                                 },
  84.                                 {
  85.                                         minLength: 3,
  86.                                         maxLength: 20,
  87.                                         errorMessage: '用户名长度在 {minLength} 到 {maxLength} 个字符',
  88.                                 }
  89.                         ]
  90.                 },
  91.                 password: {
  92.                         rules: [{
  93.                                         required: true,
  94.                                         errorMessage: '请输入密码',
  95.                                 },
  96.                                 {
  97.                                         minLength: 6,
  98.                                         maxLength: 20,
  99.                                         errorMessage: '密码长度在 {minLength} 到 {maxLength} 个字符',
  100.                                 }
  101.                         ]
  102.                 },
  103.                 password2: {
  104.                         rules: [{
  105.                                         required: true,
  106.                                         errorMessage: '请再次确认密码',
  107.                                 },
  108.                                 {
  109.                                         minLength: 6,
  110.                                         maxLength: 20,
  111.                                         errorMessage: '密码长度在 {minLength} 到 {maxLength} 个字符',
  112.                                 }
  113.                         ]
  114.                 },
  115.                 sex: {
  116.                         rules: [{
  117.                                 required: true,
  118.                                 errorMessage: '请选择性别',
  119.                         }]
  120.                 },
  121.                 phone: {
  122.                         rules: [{
  123.                                         required: true,
  124.                                         errorMessage: '请输入手机号码',
  125.                                 },
  126.                                 {
  127.                                         minLength: 11,
  128.                                         maxLength: 11,
  129.                                         errorMessage: '手机号码长度为11个字符',
  130.                                 }
  131.                         ]
  132.                 },
  133.                 captcha: {
  134.                         rules: [{
  135.                                         required: true,
  136.                                         errorMessage: '请输入验证码',
  137.                                 },
  138.                                 {
  139.                                         minLength: 6,
  140.                                         maxLength: 6,
  141.                                         errorMessage: '验证码长度为6个字符',
  142.                                 }
  143.                         ]
  144.                 },
  145.         }
  146. </script>
  147. <style lang="scss">
  148.         .reg-view {
  149.                 padding: 20px;
  150.         }
  151. </style>
复制代码
下面是带异步校验规则的案例代码:
  1. <template>
  2.         <view>
  3.                 <uni-notice-bar show-icon scrollable :text="notice" />
  4.         </view>
  5.         <view class="reg-view">
  6.                 <uni-forms ref="userFormRef" :model="userForm" :label-width="80" :rules="rules">
  7.                         <uni-forms-item label="昵称" required name="nickname">
  8.                                 <uni-easyinput v-model="userForm.nickname" placeholder="请输入昵称" />
  9.                         </uni-forms-item>
  10.                         <uni-forms-item label="用户名" required name="username">
  11.                                 <uni-easyinput v-model="userForm.username" placeholder="请输入用户名" />
  12.                         </uni-forms-item>
  13.                         <uni-forms-item label="密码" required name="password">
  14.                                 <uni-easyinput type="password" v-model="userForm.password" placeholder="请输入密码" />
  15.                         </uni-forms-item>
  16.                         <uni-forms-item label="确认密码" required name="password2">
  17.                                 <uni-easyinput type="password" v-model="userForm.password2" placeholder="请再次确认密码" />
  18.                         </uni-forms-item>
  19.                         <uni-forms-item label="性别" required name="sex">
  20.                                 <uni-data-checkbox v-model="userForm.sex" :localdata="sexs" />
  21.                         </uni-forms-item>
  22.                         <uni-forms-item label="手机号码" required name="phone">
  23.                                 <uni-easyinput v-model="userForm.phone" placeholder="请输入手机号码" />
  24.                         </uni-forms-item>
  25.                         <uni-forms-item label="验证码" required name="captcha">
  26.                                 <view style="display: flex;justify-content: space-between;">
  27.                                         <view class="" style="width: 50%;">
  28.                                                 <uni-easyinput v-model="userForm.captcha" placeholder="请输入验证码" />
  29.                                         </view>
  30.                                         <view class="" style="width: 48%;">
  31.                                                 <button type="default" style="width: 99%;height: 35px;line-height: 35px;"
  32.                                                         size="mini">获取验证码</button>
  33.                                         </view>
  34.                                 </view>
  35.                         </uni-forms-item>
  36.                 </uni-forms>
  37.                 <button type="primary" class="round-button" @tap="reg">注册</button>
  38.         </view>
  39. </template>
  40. <script setup>
  41.         import {ref} from 'vue';
  42.         const userFormRef = ref()
  43.         const reg = () => {
  44.                 userFormRef.value.validate().then(res => {
  45.                         console.log('验证通过:', res);
  46.                 }).catch(err => {
  47.                         console.log('表单错误信息:', err);
  48.                 })
  49.         }
  50.         const userForm = ref({})
  51.         const sexs = ref([{
  52.                 text: '男',
  53.                 value: 0
  54.         }, {
  55.                 text: '女',
  56.                 value: 1
  57.         }, {
  58.                 text: '保密',
  59.                 value: 2
  60.         }])
  61.         const notice = ref("明天放假了")
  62.         // 表单验证规则
  63.         const rules = {
  64.                 // 对name字段进行必填验证
  65.                 nickname: {
  66.                         rules: [{
  67.                                         required: true,
  68.                                         errorMessage: '请输入昵称',
  69.                                 },
  70.                                 {
  71.                                         minLength: 2,
  72.                                         maxLength: 50,
  73.                                         errorMessage: '昵称长度在 {minLength} 到 {maxLength} 个字符',
  74.                                 }
  75.                         ]
  76.                 },
  77.                 username: {
  78.                         rules: [{
  79.                                         required: true,
  80.                                         errorMessage: '请输入用户名',
  81.                                 },
  82.                                 {
  83.                                         minLength: 3,
  84.                                         maxLength: 20,
  85.                                         errorMessage: '用户名长度在 {minLength} 到 {maxLength} 个字符',
  86.                                 },
  87.                                 {
  88.                                         validateFunction: (rule, value, data, callback) => {
  89.                                                         // 异步需要返回 Promise 对象
  90.                                                         return new Promise((resolve, reject) => {
  91.                                                                 if(value == 'admin'){
  92.                                                                         reject(new Error('非法的用户名'))
  93.                                                                 }
  94.                                                                 resolve()
  95.                                                         })
  96.                                                 }
  97.                                         }
  98.                         ]
  99.                 },
  100.                 password: {
  101.                         rules: [{
  102.                                         required: true,
  103.                                         errorMessage: '请输入密码',
  104.                                 },
  105.                                 {
  106.                                         minLength: 6,
  107.                                         maxLength: 20,
  108.                                         errorMessage: '密码长度在 {minLength} 到 {maxLength} 个字符',
  109.                                 }
  110.                         ]
  111.                 },
  112.                 password2: {
  113.                         rules: [{
  114.                                         required: true,
  115.                                         errorMessage: '请再次确认密码',
  116.                                 },
  117.                                 {
  118.                                         minLength: 6,
  119.                                         maxLength: 20,
  120.                                         errorMessage: '密码长度在 {minLength} 到 {maxLength} 个字符',
  121.                                 },{
  122.                                                 validateFunction:function(rule,value,data,callback){
  123.                                                         // data 包含表单所有数据
  124.                                                         if (value != data.password) {
  125.                                                                 callback('两次密码输入不一致')
  126.                                                         }
  127.                                                         return true
  128.                                                 }
  129.                                         }
  130.                         ]
  131.                 },
  132.                 sex: {
  133.                         rules: [{
  134.                                 required: true,
  135.                                 errorMessage: '请选择性别',
  136.                         }]
  137.                 },
  138.                 phone: {
  139.                         rules: [{
  140.                                         required: true,
  141.                                         errorMessage: '请输入手机号码',
  142.                                 },
  143.                                 {
  144.                                         minLength: 11,
  145.                                         maxLength: 11,
  146.                                         errorMessage: '手机号码长度为11个字符',
  147.                                 },
  148.                                 {
  149.                                                 validateFunction:function(rule,value,data,callback){
  150.                                                  // 中国手机号码正则:11位,以13-19开头(含最新号段)
  151.                                                         const phoneReg = /^1[3-9]\d{9}$/;
  152.                                                         if (!phoneReg.test(value)) {
  153.                                                           callback('手机号码格式不正确,需以13-19开头的11位数字');
  154.                                                           return;
  155.                                                         }
  156.                                                 }
  157.                                         }
  158.                         ]
  159.                 },
  160.                 captcha: {
  161.                         rules: [{
  162.                                         required: true,
  163.                                         errorMessage: '请输入验证码',
  164.                                 },
  165.                                 {
  166.                                         minLength: 6,
  167.                                         maxLength: 6,
  168.                                         errorMessage: '验证码长度为6个字符',
  169.                                 },
  170.                                 {
  171.                                                 validateFunction: (rule, value, data, callback) => {
  172.                                                         // 异步需要返回 Promise 对象
  173.                                                                 return new Promise((resolve, reject) => {
  174.                                                                         if(value == '888888'){
  175.                                                                                 reject(new Error('无效验证码'))
  176.                                                                         }
  177.                                                                 resolve()
  178.                                                        
  179.                                                         })
  180.                                                 }
  181.                                         }
  182.                         ]
  183.                 },
  184.         }
  185. </script>
  186. <style lang="scss">
  187.         .reg-view {
  188.                 padding: 20px;
  189.         }
  190. </style>
复制代码


QQ|网站地图|Archiver|小黑屋|金黑网络 ( 粤ICP备2021124338号 )

网站建设,微信公众号小程序制作,商城系统开发,高端系统定制,app软件开发,智能物联网开发,直播带货系统等

Powered by Www.Jinhei.Cn

Copyright © 2013-2024 深圳市金黑网络技术有限公司 版权所有

快速回复 返回顶部 返回列表