verification.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import Vrules from "./Vregular.js"; //正则验证
  2. var verification={};
  3. verification.install=function(Vue){
  4. Vue.prototype.$vervify =function(option){
  5. /*必传项
  6. option.formDate={
  7. label:"身份证号", //传option.otherPra.reqEmptyVal=true时,label必传,反之不传或者为false时label可不传
  8. rules:{
  9. value:"",
  10. verify:"req|idcard", //"req|idcard" req为必填项(文本框是否为必填项) || "idcard" 不传req 如果文本框有内容就进行idcard规则验证,无内容则不验证
  11. errMess:"请输入身份证号" //非必传,不传用规则定义的默认提示
  12. }
  13. },
  14. // 可选项
  15. option.otherPra={
  16. reqEmptyVal:true,
  17. }
  18. */
  19. // 不传默认参数
  20. /*otherDefault.reqEmptyVal=true 必填项为空是先提示请输入**** ,默认false 直接判断是否符合规则,
  21. 不符合规则直接弹用户指定的错误,
  22. */
  23. var otherDefault={
  24. reqEmptyVal:false,
  25. }
  26. var otherPra=Object.assign({},otherDefault,option.otherPra) //合并对象
  27. var result=0;
  28. var flag=true;
  29. for(var m=0;m<option.formDate.length;m++){
  30. var regular=option.formDate[m].rules.verify.split("|");
  31. for(var i=0;i<regular.length;i++){
  32. if(Vrules[regular[i]]){
  33. var _default=Object.assign({},Vrules[regular[i]],option.formDate[m].rules) //合并对象
  34. if(regular[i]=="req"){
  35. // 必填项未填写内容提示
  36. if(otherPra.reqEmptyVal){
  37. var emptyMessage="请输入"+option.formDate[m].label;
  38. if(option.formDate[m].type=="radio"){
  39. emptyMessage="请选择"+option.formDate[m].label;
  40. }else if(option.formDate[m].type=="image"){
  41. emptyMessage="请上传"+option.formDate[m].label;
  42. }
  43. if(_default.value.length==0){
  44. uni.showToast({
  45. title:emptyMessage,
  46. duration: 2000,
  47. icon:"none"
  48. });
  49. flag=false;
  50. }else{
  51. regularVer()
  52. }
  53. }else{
  54. regularVer()
  55. }
  56. }else if(regular[i]=="same"){//确认输入框需要配置在被确认的下面
  57. if(m>0){
  58. if(option.formDate[m-1].rules.value!=_default.value){
  59. uni.showToast({
  60. title: _default.errMess,
  61. duration: 2000,
  62. icon:"none"
  63. });
  64. flag=false;
  65. }
  66. }else{
  67. uni.showToast({
  68. title: "表单配置不正确,请检查被确认输入框的位置",
  69. duration: 2000,
  70. icon:"none"
  71. });
  72. flag=false;
  73. }
  74. }else{
  75. if(_default.value.length>0){
  76. regularVer()
  77. }
  78. }
  79. function regularVer(){
  80. if(!_default.pattern.test( _default.value)){
  81. uni.showToast({
  82. title: _default.errMess,
  83. duration: 2000,
  84. icon:"none"
  85. });
  86. flag=false;
  87. }else{
  88. flag=true;
  89. }
  90. }
  91. if(!flag){
  92. return flag
  93. }
  94. }
  95. }
  96. }
  97. return flag;
  98. },
  99. // 提交序列化的表单
  100. Vue.prototype.$submitForm =function(option){
  101. var submitData={};
  102. for(var i=0;i<option.length;i++){
  103. submitData[option[i].rules.name]=option[i].rules.value;
  104. }
  105. return submitData;
  106. }
  107. }
  108. export default verification