{"id":3193,"date":"2024-11-10T15:51:52","date_gmt":"2024-11-10T07:51:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3193\/"},"modified":"2024-11-10T15:51:52","modified_gmt":"2024-11-10T07:51:52","slug":"js%e5%a6%82%e4%bd%95%e9%aa%8c%e8%af%81%e9%82%ae%e7%ae%b1","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3193\/","title":{"rendered":"js\u5982\u4f55\u9a8c\u8bc1\u90ae\u7bb1"},"content":{"rendered":"<blockquote><p>\n  \u5728 javascript \u4e2d\u9a8c\u8bc1\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5b9e\u73b0\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6709\u6548\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u683c\u5f0f\u3002html5 \u8f93\u5165\u9a8c\u8bc1\uff1a\u4f7f\u7528 pattern \u5c5e\u6027\u5728\u8868\u5355\u5b57\u6bb5\u4e2d\u9a8c\u8bc1\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u3002\u5f02\u6b65\u9a8c\u8bc1\uff1a\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u6216 api \u8fdb\u884c\u5f02\u6b65\u7535\u5b50\u90ae\u4ef6\u9a8c\u8bc1\uff0c\u53ef\u4ee5\u9a8c\u8bc1\u90ae\u7bb1\u662f\u5426\u5b58\u5728\u6216\u80fd\u5426\u63a5\u6536\u7535\u5b50\u90ae\u4ef6\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110806455015239.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u9a8c\u8bc1\u90ae\u7bb1\u63d2\u56fe\" alt=\"js\u5982\u4f55\u9a8c\u8bc1\u90ae\u7bb1\u63d2\u56fe\" \/><\/p>\n<p><strong>JS \u9a8c\u8bc1\u7535\u5b50\u90ae\u4ef6<\/strong><\/p>\n<p>\u7535\u5b50\u90ae\u4ef6\u9a8c\u8bc1\u5728\u786e\u4fdd\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u5408\u6cd5\u6027\u65b9\u9762\u81f3\u5173\u91cd\u8981\uff0c\u5728 JavaScript \u4e2d\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u3002<\/p>\n<p><strong>1. \u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u6709\u6548\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u683c\u5f0f\uff1a<\/p>\n<pre>const re = \/^(([^&lt;&gt;()[]\\.,;:s@\"]+(.[^&lt;&gt;()[]\\.,;:s@\"]+)*)|(\".+\"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$\/;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8981\u4f7f\u7528\u6b64\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u8bf7\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a RegExp \u5bf9\u8c61\uff1aconst pattern = new RegExp(re);<\/li>\n<li>\u4f7f\u7528 test() \u65b9\u6cd5\u6765\u6d4b\u8bd5\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff1apattern.test(emlAddress);<\/li>\n<\/ul>\n<p><strong>2. HTML5 \u8f93\u5165\u9a8c\u8bc1<\/strong><\/p>\n<p>HTML5 \u63d0\u4f9b\u4e86 pattern \u5c5e\u6027\uff0c\u53ef\u4ee5\u7528\u6765\u5728\u8868\u5355\u5b57\u6bb5\u4e2d\u9a8c\u8bc1\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff1a<\/p>\n<pre>&lt;input type=\"email\" pattern=\"^(([^&lt;&gt;()[]\\.,;:s@\"]+(.[^&lt;&gt;()[]\\.,;:s@\"]+)*)|(\".+\"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$\"&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u5f02\u6b65\u9a8c\u8bc1<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u6216 API \u6765\u8fdb\u884c\u5f02\u6b65\u7535\u5b50\u90ae\u4ef6\u9a8c\u8bc1\u3002\u8fd9\u5bf9\u4e8e\u9a8c\u8bc1\u662f\u5426\u5b58\u5728\u6216\u662f\u5426\u80fd\u591f\u63a5\u6536\u7535\u5b50\u90ae\u4ef6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 emailjs \u5e93\uff1a<\/p>\n<pre>emailjs.init(\"YOUR_API_KEY\");\nemailjs.send(\"YOUR_SERVICE_ID\", \"YOUR_TEMPLATE_ID\", {\n  email: emailAddress,\n}, (err, response) =&gt; {\n  if (err) {\n    \/\/ \u9a8c\u8bc1\u5931\u8d25\n  } else {\n    \/\/ \u9a8c\u8bc1\u6210\u529f\n  }\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728 JavaScript \u4e2d\u8f7b\u677e\u9a8c\u8bc1\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff0c\u4ece\u800c\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u7535\u5b50\u90ae\u4ef6\u4fe1\u606f\u7684\u51c6\u786e\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u9a8c\u8bc1\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 javascript \u4e2d\u9a8c\u8bc1\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5b9e\u73b0\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6709\u6548\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u683c\u5f0f\u3002html5 \u8f93\u5165\u9a8c\u8bc1\uff1a\u4f7f\u7528 pattern \u5c5e\u6027\u5728\u8868\u5355\u5b57\u6bb5\u4e2d\u9a8c\u8bc1\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u3002\u5f02\u6b65\u9a8c\u8bc1\uff1a\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u6216 api \u8fdb\u884c\u5f02\u6b65\u7535\u5b50\u90ae\u4ef6\u9a8c\u8bc1\uff0c\u53ef\u4ee5\u9a8c\u8bc1\u90ae\u7bb1\u662f\u5426\u5b58\u5728\u6216\u80fd\u5426\u63a5\u6536\u7535\u5b50\u90ae\u4ef6\u3002 JS \u9a8c\u8bc1\u7535\u5b50\u90ae\u4ef6 \u7535\u5b50\u90ae\u4ef6\u9a8c\u8bc1\u5728\u786e\u4fdd\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u5408\u6cd5\u6027\u65b9\u9762\u81f3\u5173\u91cd\u8981\uff0c\u5728 JavaScript \u4e2d\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u3002 1. \u6b63\u5219\u8868\u8fbe\u5f0f \u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u6709\u6548\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u683c\u5f0f\uff1a const re = \/^(([^&lt;&gt;()[]\\.,;:s@&#8221;]+(.[^&lt;&gt;()[]\\.,;:s@&#8221;]+)*)|(&#8220;.+&#8221;))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$\/; \u767b\u5f55\u540e\u590d\u5236 \u8981\u4f7f\u7528\u6b64\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u8bf7\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u64cd\u4f5c\uff1a \u521b\u5efa\u4e00\u4e2a RegExp \u5bf9\u8c61\uff1aconst pattern = new RegExp(re); \u4f7f\u7528 test() \u65b9\u6cd5\u6765\u6d4b\u8bd5\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff1apattern.test(emlAddress); 2. HTML5 \u8f93\u5165\u9a8c\u8bc1 HTML5 \u63d0\u4f9b\u4e86 pattern \u5c5e\u6027\uff0c\u53ef\u4ee5\u7528\u6765\u5728\u8868\u5355\u5b57\u6bb5\u4e2d\u9a8c\u8bc1\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff1a &lt;input type=&#8221;email&#8221; pattern=&#8221;^(([^&lt;&gt;()[]\\.,;:s@&#8221;]+(.[^&lt;&gt;()[]\\.,;:s@&#8221;]+)*)|(&#8220;.+&#8221;))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$&#8221;&gt; \u767b\u5f55\u540e\u590d\u5236 3. \u5f02\u6b65\u9a8c\u8bc1 \u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u6216 API \u6765\u8fdb\u884c\u5f02\u6b65\u7535\u5b50\u90ae\u4ef6\u9a8c\u8bc1\u3002\u8fd9\u5bf9\u4e8e\u9a8c\u8bc1\u662f\u5426\u5b58\u5728\u6216\u662f\u5426\u80fd\u591f\u63a5\u6536\u7535\u5b50\u90ae\u4ef6\u975e\u5e38\u6709\u7528\u3002 \u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 emailjs \u5e93\uff1a emailjs.init(&#8220;YOUR_API_KEY&#8221;); emailjs.send(&#8220;YOUR_SERVICE_ID&#8221;, &#8220;YOUR_TEMPLATE_ID&#8221;, { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3193","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3193"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3193\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}