edit.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <template>
  2. <view class="container">
  3. <!-- 手机号码输入框 -->
  4. <view class="input-group">
  5. <text>手机号码:</text>
  6. <input type="number" maxlength="11" v-model="userInfo.phone" placeholder="请输入手机号码" />
  7. </view>
  8. <!-- 邮箱输入框 -->
  9. <view class="input-group">
  10. <text>邮箱:</text>
  11. <input type="text" v-model="userInfo.email" placeholder="请输入邮箱地址" />
  12. </view>
  13. <!-- 用户生日输入框 -->
  14. <view class="input-group">
  15. <text>用户生日:</text>
  16. <view class="example-body">
  17. <uni-datetime-picker type="date" :clear-icon="false" v-model="userInfo.birthday" @maskClick="maskClick" />
  18. </view>
  19. </view>
  20. <!-- 性别选择器 -->
  21. <view class="gender-selector">
  22. <radio-group @change="handleGenderChange">
  23. <text>性别:</text>
  24. <view class="genderLabel">
  25. <label>
  26. <radio value="male" :checked="userInfo.gender === 'male' " />男
  27. </label>
  28. <label>
  29. <radio value="female" :checked="userInfo.gender === 'female'" />女
  30. </label>
  31. </view>
  32. </radio-group>
  33. </view>
  34. <!-- 提交按钮 -->
  35. <view class="primaryBtn">
  36. <button type="primary" @click="handleSubmit">提交</button>
  37. </view>
  38. </view>
  39. </template>
  40. <script setup>
  41. import {
  42. ref,
  43. reactive
  44. } from 'vue';
  45. // 定义用户信息的响应式对象
  46. const userInfo = reactive({
  47. phone: '13145672389',
  48. email: '',
  49. gender: 'male',
  50. birthday: '2000-01-10'
  51. });
  52. function handleGenderChange(event) {
  53. userInfo.gender = event.detail.value; // 更新性别
  54. };
  55. // 校验手机号码格式
  56. function validatePhone(phone) {
  57. const phoneRegex = /^1[3-9]\d{9}$/; // 手机号码格式正则(以1开头,11位数字)
  58. return phoneRegex.test(phone);
  59. }
  60. // 校验邮箱格式
  61. function validateEmail(email) {
  62. const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; // 邮箱格式正则
  63. return emailRegex.test(email);
  64. }
  65. // 处理提交事件
  66. function handleSubmit() {
  67. if (!validatePhone(userInfo.phone)) {
  68. uni.showToast({
  69. title: '手机号码格式不正确',
  70. icon: 'none'
  71. });
  72. return; // 终止提交
  73. }
  74. if (!validateEmail(userInfo.email)) {
  75. uni.showToast({
  76. title: '邮箱格式不正确',
  77. icon: 'none'
  78. });
  79. return; // 终止提交
  80. }
  81. uni.showToast({
  82. title: '提交成功',
  83. icon: 'none'
  84. });
  85. }
  86. </script>
  87. <style lang="scss" scoped>
  88. .container {
  89. padding: 30rpx;
  90. }
  91. .input-group {
  92. margin-bottom: 10rpx;
  93. }
  94. .input-group input,
  95. .genderLabel {
  96. padding-left: 20rpx;
  97. margin: 20rpx auto;
  98. height: 70rpx;
  99. }
  100. .input-group input {
  101. border: 1rpx solid gainsboro; /* 修改边框样式 */
  102. background-color: white;
  103. }
  104. .input-group text {
  105. display: block;
  106. margin-bottom: 20rpx;
  107. }
  108. .gender-selector label {
  109. margin-right: 15rpx;
  110. }
  111. .primaryBtn {
  112. margin-top: 70rpx;
  113. }
  114. </style>