当前位置:   article > 正文

微信小程序 表单 验证_actbanner

actbanner

表单分为两个主要部分,一个是客户提交的信息,一个是主要内容(内含文字和上传图片),完整源码见上一篇

功能:点击文字则向上划出文本输入框(点击完成的时候做判断,内容不为空),点击图片则打开上传图片,

思路:

1.前端布局给两个按钮  添加文字  bindtap='addTextTap'    上传图片   bindtap='chooseImg' 

   一个隐藏的视窗   通过textreaHidden来控制是否显示视窗

  1. <!--pages/activity/signup/signup.wxml-->
  2. <!-- banner图 -->
  3. <view class='bg_white' style='padding-bottom:30rpx;'>
  4. <image src='/images/actbanner.jpg' style='width:100%;height:220rpx;'></image>
  5. </view>
  6. <!-- 表单主要内容 -->
  7. <view class='p_b20' style='width:90%;margin:0 auto;'>
  8. <form bindsubmit="formSubmit">
  9. <view class='addview20' style='padding-bottom:90rpx;'>
  10. <view class='bg_white font14 gray5 p_all10 p_b20'style='width:95%;margin 0 auto;padding-top:20rpx;'>
  11. <view class='inputstyle'>
  12. <picker bindchange="bindPickerzone" value="{{index2}}" range="{{userZoneList}}">
  13. <input class='fl' type='text' placeholder='所在城市' placeholder-class='place' name="city" disabled='disabled' value='{{userZoneList[index2]}}'></input>
  14. <text class='fr m_t5 grayb'style='margin-right:20rpx;'></text>
  15. <view class='cl'></view>
  16. </picker>
  17. </view>
  18. <input type='number' class='inputstyle' placeholder-class='place' name="area" placeholder='房屋面积(㎡)'></input>
  19. <input type='number' class='inputstyle' maxlength='11' placeholder-class='place' name="phoneNumber" placeholder='手机号'></input>
  20. <input type='text' class='inputstyle' placeholder-class='place' name="userName" placeholder='姓名'></input>
  21. </view>
  22. <!-- 正文内容 -->
  23. <view class='p_lr15' wx:if="{{content.length > 0}}">
  24. <view class='contentView' wx:for="{{content}}">
  25. <view class='m_t30 pr' wx:if="{{item.name == 'img'}}">
  26. <image src='{{item.contents}}' mode='widthFix' style='width:100%;'></image>
  27. <view class='delet pa tc font22 white bg_grayc' data-index='{{index}}' bindtap='deletTap'><text>×</text></view>
  28. </view>
  29. <view class='font15 gray2 tj lineH_m text p_all10 m_t30 pr' wx:if="{{item.name == 'text'}}">
  30. <text>{{item.contents}}</text>
  31. <view class='delet pa font22 white bg_grayc' data-index='{{index}}' bindtap='deletTap'><text>×</text></view>
  32. <view class='edit pa bg_grayc tc' data-index='{{index}}' bindtap='editTap'>
  33. <image src='/images/icon/edit.png' style='width:26rpx;height:26rpx;margin-top:12rpx'></image>
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. <view class='tc p_tb15'>
  39. <text class='block font15 gray9' wx:if="{{content.length <= 0}}">开始添加内容</text>
  40. <view class='m_t10' bindtap='addTap'><text class='addto bg_graye white font24' bindtap='addtoTap'>+</text></view>
  41. <view class='inline_block p_tb5 p_lr30 addview m_t10' wx:if="{{addHidden == false}}">
  42. <view class='inline_block m_r20' bindtap='addTextTap'>
  43. <image src='/images/icon/fa_03.png' style='width:40rpx;height:40rpx;'></image>
  44. <text class='block font14 gray2 tc'>文字</text>
  45. </view>
  46. <view class='inline_block' bindtap='chooseImg'>
  47. <image src='/images/icon/fa_05.png' style='width:40rpx;height:40rpx;'></image>
  48. <text class='block font14 gray2 tc'>图片</text>
  49. </view>
  50. </view>
  51. </view>
  52. <view class='pf textrea bg_white {{textreaHidden == true ? "animation_huachu" : "animation_huaru"}}' wx:if="{{textreaHidden == false}}">
  53. <form bindsubmit="textreaSubmit">
  54. <view class='p_all15 bd_b'style='min-height:300rpx;'>
  55. <textarea value='{{editVal}}' adjust-position="true" maxlength='-1' name="textarea" placeholder='请输入内容……' auto-height='true' class='font15 gray2'></textarea>
  56. </view>
  57. <view class='tr p_lr15'>
  58. <button class='green font16' form-type="submit">
  59. <text>完成</text>
  60. </button>
  61. </view>
  62. </form>
  63. </view>
  64. </view>
  65. <!-- 提交报名 -->
  66. <view class='tr p_lr15 bg_white' style='margin-top:30rpx;'>
  67. <button class='form_button' form-type="submit">
  68. <text class='p_tb5 p_lr20 bg_theme white font16 tc block' style='border-radius:15rpx;width:100%;height:80rpx;line-height:60rpx;margin: 0 auto;'>提交信息</text>
  69. </button>
  70. </view>
  71. </form>
  72. </view>

 data中的初始数据 

  1. data: {
  2. sortCurrent: '',//
  3. upload_limit: 1,// 默认最多上传9张
  4. img_srcs: [], //如果是编辑状态,只需要把原信息的图片地址放到此处就可以显示出来
  5. img_src: [],
  6. //is_designer: wx.getStorageSync('is_designer'),//是否是设计师
  7. content: [],//正文内容
  8. textreaHidden: true,
  9. addHidden: false,
  10. editVal: '',//编辑文字内容
  11. editIndex: '',//编辑的index
  12. userZoneList: [],//城市列表
  13. index2: '',//城市下标
  14. },

2.  在js中给textreaHidden初始值未true(即 正文内容输入框默认隐藏)

    点击添加文字   textreaHidden赋值为false  (即 正文内容输入框显示)     

  1. addTextTap: function (e) {
  2. var that = this;
  3. that.setData({
  4. textreaHidden: false,
  5. })
  6. },

  输入文字后点击完成(此处需要判断文字内容是否为空) ,为空的话弹框提示返回false

  1. //文字textrea提交
  2. textreaSubmit: function (e) {
  3. var that = this;
  4. console.log(e.detail.value.textarea)
  5. var j = that.data.content.length;
  6. var content = that.data.content;
  7. var value = e.detail.value.textarea;
  8. console.log(value);//上传的内容
  9. //判断内容是否为空
  10. if (value !== "") {
  11. if (that.data.editIndex !== '') {
  12. //editIndex
  13. content[that.data.editIndex].contents = value;
  14. } else {
  15. content[j] = {
  16. "name": "text",
  17. "contents": value
  18. };
  19. }
  20. that.setData({
  21. textreaHidden: true,
  22. content: content,
  23. addHidden: true
  24. })
  25. console.log(that.data.content)
  26. } else {
  27. wx.showModal({
  28. title: '提示',
  29. content: '内容不能为空,请输入内容',
  30. success: function (res) {
  31. if (res.confirm) {
  32. return false;
  33. } else {
  34. that.setData({
  35. textreaHidden: true,
  36. })
  37. }
  38. }
  39. })
  40. }
  41. },

 

    点击上传图片

  1. chooseImg: function (e) {
  2. var that = this;
  3. wx.chooseImage({
  4. count: that.data.upload_limit,
  5. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  6. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  7. success: function (res) {
  8. // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
  9. var tempFilePaths = res.tempFilePaths;
  10. console.log(tempFilePaths)
  11. that.sendPhotos(tempFilePaths);
  12. console.log(that.data.content);
  13. }
  14. })
  15. },

将图片发送给后端服务期,预览图片,删除模块 

  1. //发图片发送给后端服务器
  2. sendPhotos: function (tempFilePaths) {
  3. var that = this
  4. wx.showNavigationBarLoading();
  5. console.log(tempFilePaths[0])
  6. if (tempFilePaths.length !== 0) {
  7. wx.uploadFile({
  8. url: app.d.ceshiUrl + 'comment.uploadPic',
  9. filePath: tempFilePaths[0],
  10. name: 'picture',
  11. header: { "Content-Type": "multipart/form-data" },
  12. success: function (res) {
  13. console.log(res.data)
  14. var data = JSON.parse(res.data)
  15. console.log(data)
  16. var j = that.data.content.length;
  17. var content = that.data.content;
  18. if (data !== '') {
  19. var path = data.path;
  20. content[j] = {
  21. "name": "img",
  22. "contents": "https://xcx.fczxwl.com/attachment/" + path
  23. };
  24. }
  25. that.setData({
  26. content: content,
  27. addHidden: true
  28. });
  29. console.log(that.data.content)
  30. wx.hideNavigationBarLoading();
  31. tempFilePaths.splice(0, 1);
  32. // that.sendPhotos(tempFilePaths);
  33. },
  34. fail: function (res) {
  35. console.log('上传图片到服务器失败');
  36. },
  37. complete: function (res) {
  38. console.log(res);
  39. }
  40. })
  41. }
  42. },
  43. //图片预览
  44. previewImage: function (e) {
  45. var img_srcs = this.data.img_srcs;
  46. var index = e.target.dataset.index;
  47. wx.previewImage({
  48. current: img_srcs[index],
  49. urls: img_srcs // 需要预览的图片http链接列表
  50. })
  51. },
  52. //删除内容快
  53. deletTap: function (e) {
  54. var that = this;
  55. console.log(e)
  56. var index = e.currentTarget.dataset.index;
  57. var content = that.data.content;
  58. wx.showModal({
  59. title: '提示',
  60. content: '确定要删除?',
  61. success: function (res) {
  62. if (res.confirm) {
  63. content.splice(index, 1)
  64. that.setData({
  65. content: content,
  66. })
  67. }
  68. }
  69. })
  70. },

 

提交所有信息的js判断验证

  1. /**
  2. * 页面的初始数据
  3. */
  4. data: {
  5. sortCurrent: '',//
  6. upload_limit: 1,// 默认最多上传9张
  7. img_srcs: [], //如果是编辑状态,只需要把原信息的图片地址放到此处就可以显示出来
  8. img_src: [],
  9. content: [],//正文内容
  10. textreaHidden: true,
  11. addHidden: false,
  12. editVal: '',//编辑文字内容
  13. editIndex: '',//编辑的index
  14. userZoneList: [],//城市列表
  15. index2: '',//城市下标
  16. },
  17. //发布提交信息
  18. formSubmit: function (e) {
  19. var that = this;
  20. console.log(that.data);
  21. var userInfo = wx.getStorageSync('userInfo'); //用户基本信息
  22. //console.log(e.detail.value) //表单中input的值
  23. var city = e.detail.value.city; //所在城市
  24. var area = e.detail.value.area; //房屋面积
  25. var phoneNumber = e.detail.value.phoneNumber; //手机号
  26. var userName = e.detail.value.userName; //姓名
  27. var arr = { //将正文内容传到data中
  28. "content": that.data.content
  29. }
  30. var mobile = /^[1][3,4,5,7,8][0-9]{9}$/;
  31. var isMobile = mobile.exec(e.detail.value.phoneNumber) //校验手机号
  32. //所有消息有任一为空时
  33. if (city == "" || area=="" || phoneNumber=="" || userName=="") {
  34. wx.showModal({
  35. title: '发布提示',
  36. content: '您的信息缺失,请重新填写!',
  37. showCancel: false,
  38. success: function (res) {
  39. if (res.confirm) {
  40. return false;
  41. }
  42. }
  43. })
  44. wx.hideNavigationBarLoading();
  45. }
  46. //判断电话号格式是否正确
  47. else if (!isMobile) {
  48. console.log('form表单发生点击事件,携带的数据为:', e.detail.value)
  49. console.log('电话:', e.detail.value.phoneNumber)
  50. wx.showModal({
  51. title: '提示!!',
  52. content: '你输入的电话不符,请重新检查填写',
  53. })
  54. }
  55. //当所有消息不为空
  56. else {
  57. //判断主要内容
  58. console.log(arr.content.length);
  59. if (arr.content.length == 0){
  60. wx.showModal({
  61. title: '发布提示',
  62. content: '文章内容不能为空,请编辑内容',
  63. showCancel: false,
  64. success: function (res) {
  65. if (res.confirm) {
  66. return false;
  67. }
  68. }
  69. })
  70. wx.hideNavigationBarLoading();
  71. }else{
  72. wx.request({
  73. url: app.d.hostUrl + 'activity.addApply',
  74. data: {
  75. "openid": wx.getStorageSync('openid'),
  76. "nickname": userInfo.nickName, //昵称
  77. "avatar": userInfo.avatarUrl, //头像
  78. "name": e.detail.value.userName, //姓名
  79. "zone": e.detail.value.city, //城市
  80. "area": e.detail.value.area, //手机号
  81. "tel": e.detail.value.phoneNumber, //手机号
  82. "content": arr, //内容
  83. },
  84. method: 'GET',
  85. header: {
  86. 'Accept': 'application/json'
  87. },
  88. success: function (res) {
  89. console.log(res)
  90. wx.hideNavigationBarLoading();
  91. wx.showToast({
  92. title: '已提交报名',
  93. icon: 'success',
  94. duration: 2000,
  95. success: function () {
  96. setTimeout(function () {
  97. wx.reLaunch({
  98. url: '/pages/activity/home/home',
  99. })
  100. }, 2000)
  101. }
  102. })
  103. }
  104. })
  105. }
  106. }
  107. },

 啊 !!!写不下去了,以后再补充吧,欢迎路过的大神补充说明,不胜感激(下篇附上表单验证所有源码,语文不好,唉 !)

 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号