{"id":227702,"date":"2021-10-09T10:44:53","date_gmt":"2021-10-09T02:44:53","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=227702"},"modified":"2021-09-30T09:45:32","modified_gmt":"2021-09-30T01:45:32","slug":"vue-form-regexp","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/vue-form-regexp.html","title":{"rendered":"vue\u8868\u5355\u6821\u9a8c\u65b9\u6cd5"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u7684\u662fvue \u5982\u4f55\u5b9e\u73b0\u8868\u5355\u6821\u9a8c\u7684\u65b9\u6cd5\uff0c\u6709\u4ee3\u7801\u8be6\u7ec6\u89e3\u8bf4\uff0c\u611f\u5174\u8da3\u7684\u5c0f\u4f19\u4f34\u53ef\u4ee5\u53c2\u8003\u4e0b\u9762\u6587\u7ae0\u7684\u5177\u4f53\u5185\u5bb9<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u4e00\u3001\u5b89\u88c5\u5e76\u4f7f\u7528<\/strong><\/div>\n

\u9996\u5148\uff0c\u5728\u4f60\u7684vue\u9879\u76ee\u4e2d\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n

\r\nnpm install --save vue-input-check\r\n<\/pre>\n

\u5b89\u88c5\u5b8c\u6210\u4ee5\u540e\u5f15\u5165\u5e76\u6ce8\u518c\uff1a<\/p>\n

\r\nimport inputCheck from 'vue-input-check';\r\n \r\n\/\/ \u5b89\u88c5\r\nVue.use(inputCheck);\r\n<\/pre>\n

\u7136\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u8868\u5355\u4e2d\u4f7f\u7528\u4e86\uff1a<\/p>\n

\r\n<form autocomplete=\"off\" novalidate>\r\n    <input v-model='key' name='\u8f93\u5165\u6846\u540d\u79f0' v-input-check='[key,\"validate-express\"]'\/>\r\n    <!-- \u53ef\u4ee5\u6709\u4efb\u610f\u591a\u7684\u8f93\u5165\u6846 -->\r\n<\/form>\r\n<\/pre>\n

\u5982\u4f60\u6240\u89c1\uff0c\u4e0a\u8ff0\u7684v-input-check\u5c31\u662f\u6211\u4eec\u5bf9\u6bcf\u4e2a\u8f93\u5165\u6846\u5b9a\u4e49\u89c4\u5219\u7684\u5730\u65b9\uff0c\u503c\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u7b2c\u4e00\u4e2a\u503c\u5c31\u662f\u8f93\u5165\u6846\u7684v-model\uff0c\u7b2c\u4e8c\u4e2a\u503c\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n

\r\nvalidate-express=\"val1:param1:param2|val2|valu3:param1\"\r\n<\/pre>\n

\u4e0d\u540c\u7684\u89c4\u5219\u4f7f\u7528|\u5206\u5272\uff0c\u9700\u8981\u4f20\u9012\u53c2\u6570\u7684\u89c4\u5219\u7684\u53c2\u6570\u901a\u8fc7:\u5206\u5272\u3002\u6211\u4eec\u6765\u770b\u51e0\u4e2a\u4f8b\u5b50\uff1a<\/p>\n

\r\nv-input-check='[key,\"required|maxLength:10|regexp:^\\\\d{1,5}$\"]'\r\nv-input-check='[key,\"required\"]'\r\n<\/pre>\n

\u76ee\u524d\u53ef\u9009\u7684\u5185\u7f6e\u89c4\u5219\u5982\u4e0b\uff1a<\/p>\n

    \n
  • required:boolean:\u8868\u793a\u5fc5\u8f93\uff0c\u6709\u4e00\u4e2a\u53ef\u9009\u53c2\u6570\uff0c\u8868\u793a\u662f\u5426\u5fc5\u8f93\uff0c\u9ed8\u8ba4true<\/li>\n
  • maxLength:num:\u6700\u5927\u957f\u5ea6<\/li>\n
  • minLength:num:\u6700\u5c0f\u957f\u5ea6<\/li>\n
  • regexp:str:\u6b63\u5219\u8868\u8fbe\u5f0f<\/li>\n<\/ul>\n
    \u4e8c\u3001\u83b7\u53d6\u6821\u9a8c\u7ed3\u679c<\/strong><\/div>\n

    \u9875\u9762\u7684\u89c4\u5219\u5b9a\u4e49\u597d\u4e86\u4ee5\u540e\uff0c\u4f60\u6709\u4e24\u4e2d\u65b9\u5f0f\u83b7\u53d6\u6821\u9a8c\u7684\u7ed3\u679c\u3002<\/p>\n

    1.JS\u7684\u65b9\u5f0f<\/strong><\/span><\/div>\n

    \u76f4\u63a5\u4f7f\u7528\u4e0b\u5217\u65b9\u6cd5\u542f\u52a8\u68c0\u67e5\u5373\u53ef\uff1a<\/p>\n

    \r\nthis.$validateCheck(formnode, callback, errorback);\r\n<\/pre>\n

    \u6b64\u5bf9\u8c61\u5305\u542b\u4e09\u4e2a\u53c2\u6570\uff1a<\/p>\n

      \n
    • formnode\uff1a\u9700\u8981\u6821\u9a8c\u7684\u8868\u5355\u7ed3\u70b9\uff0c\u5fc5\u8f93<\/li>\n
    • callback\uff1a\u8868\u5355\u5408\u6cd5\u56de\u8c03\uff0c\u53ef\u9009<\/li>\n
    • errorback\uff1a\u8868\u5355\u975e\u6cd5\u56de\u8c03\uff0c\u53ef\u9009<\/li>\n<\/ul>\n

      \u6b64\u5916\uff0c\u9519\u8bef\u56de\u8c03\u6709\u4e00\u4e2a\u5f62\u53c2\uff0c\u6570\u636e\u683c\u5f0f\u4e3a\uff1a<\/p>\n

      \r\n{\r\n    \"$el\":\u9519\u8bef\u7684\u8f93\u5165\u6846\u7ed3\u70b9\r\n    \"$error\":\u5f53\u524d\u8f93\u5165\u6846\u7684\u7b2c\u4e00\u4e2a\u9519\u8bef\u63d0\u793a\u4fe1\u606f\r\n}\r\n<\/pre>\n
      2.HTML\u7684\u65b9\u5f0f<\/strong><\/span><\/div>\n

      \u63d0\u4f9b\u8fd9\u79cd\u65b9\u5f0f\u7684\u76ee\u7684\u662f\u4e3a\u4e86\u53ef\u4ee5\u5728\u9875\u9762\u5b9e\u65f6\u53cd\u9988\u5f53\u524d\u8868\u5355\u7684\u8f93\u5165\u60c5\u51b5\u3002<\/p>\n

      \u9996\u5148\uff0c\u5728\u8868\u5355\u4e0a\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5224\u65adclass\u5305\u542bv-valid\u6216\u8005v-invalid\u6765\u5224\u65ad\u8868\u5355\u662f\u5426\u5408\u6cd5\u3002<\/p>\n

      \u540c\u6837\u7684\uff0c\u6dfb\u52a0\u6307\u4ee4v-input-check\u7684\u5730\u65b9\u540c\u6837\u53ef\u4ee5\u8fd9\u6837\u5224\u65ad\u8be5\u5904\u662f\u5426\u5408\u6cd5\uff0c\u800c\u5bf9\u4e8e\u66f4\u5177\u4f53\u7684\u9519\u8bef\u7ec6\u8282\uff0c\u6bd4\u5982\u5fc5\u8f93\u975e\u6cd5\uff0cclass\u5c31\u4f1a\u50cf\u8fd9\u6837v-invalid-required v-invalid\u3002<\/p>\n

      \u4e09\u3001\u81ea\u5b9a\u4e49\u6821\u9a8c\u89c4\u5219<\/strong><\/div>\n

      \u5728\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u8fd8\u53ef\u80fd\u9700\u8981\u6dfb\u52a0\u65b0\u7684\u6821\u9a8c\u89c4\u5219\uff0c\u6bd5\u7adf\u9ed8\u8ba4\u7684\u5f80\u5f80\u4e0d\u8db3\u4ee5\u6ee1\u8db3\u6240\u6709\u4e1a\u52a1\u60c5\u51b5\uff1a<\/p>\n

      \r\nVue.use(inputCheck, {\r\n \r\n    \/\/ \u81ea\u5b9a\u4e49\u6821\u9a8c\u89c4\u5219\r\n    validate: [{\r\n \r\n        \/\/ \u89c4\u5219\u7684\u540d\u79f0\r\n        name: \"XXX\",\r\n \r\n        \/\/ \u6821\u9a8c\u65b9\u6cd5\uff0c\u8fd4\u56detrue\u8868\u793a\u5408\u6cd5\uff0cfalse\u8868\u793a\u975e\u6cd5\r\n        \/\/ \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u4e2a\u51fd\u6570\u9664\u4e86el\u548cval\u4e00\u5b9a\u5b58\u5728\u5916\uff0c\u4f59\u4e0b\u7684\u53c2\u6570\u662f\u4f7f\u7528\u7684\u65f6\u5019\u901a\u8fc7```:```\u5206\u5272\u4f20\u9012\u7684\uff0c\u53ef\u4ee5\u6709\u4efb\u610f\u591a\u4e2a\r\n        \/\/ \u6bd4\u5982\uff1a``` required:true|phone:parm1:param2 ```\r\n        test: function (el, val, ...) {\r\n            return true|false;\r\n        },\r\n \r\n        \/\/ \u975e\u6cd5\u63d0\u793a\u4fe1\u606f\uff0c\u5e94\u8be5\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\r\n        message: function (el, name) {\r\n            return \"XXX\";\r\n        }\r\n    },\r\n    \/\/ \u6821\u9a8c\u89c4\u5219\u53ef\u4ee5\u6709\u591a\u6761\r\n    ......\r\n    ]\r\n \r\n});\r\n<\/pre>\n

      \u5230\u6b64\u8fd9\u7bc7\u5173\u4e8evue \u5982\u4f55\u5b9e\u73b0\u8868\u5355\u6821\u9a8c\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"

      \u9996\u5148\uff0c\u5728\u4f60\u7684vue\u9879\u76ee\u4e2d\u8fdb\u884c\u5b89\u88c5\uff1a npm install –save vue-input-check \u5b89\u88c5 […]<\/p>\n","protected":false},"author":362,"featured_media":227706,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-227702","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/227702","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=227702"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/227702\/revisions"}],"predecessor-version":[{"id":227709,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/227702\/revisions\/227709"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/227706"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=227702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=227702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=227702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}