送货区域的 Javascript 邮政编码表格?

发布于 2024-10-14 19:36:06 字数 290 浏览 2 评论 0原文

我是一个 Javascript 菜鸟,下学期将参加课程并学习它,但与此同时,我有一个为本地企业做的基本网站。在它上面,我需要制作一个只有一个输入和一个提交按钮的表单,潜在客户可以在其中输入他们的邮政编码,它会说明他们是否在送货区域。我有大约 30 个邮政编码的列表。

有人可以指出我在哪里可以快速学习此功能的正确方向吗?我假设我只需要将这些 zip 输入到某个数组或类似的内容中,然后我就可以进行提交操作检查是否匹配。我理解逻辑,但我不知道从哪里开始编写代码,因为我目前只是一个 html/css 静态网页人员。

预先非常感谢。 戴夫

I'm a total Javascript noob and will be taking a class next semester and learning it but in the mean time, I have a basic website Im doing for a local business. On it I need to make a form with just one input and a submit button that a potential customer can enter their zip code into and it will say whether or not they are in the delivery area. I have a list of about 30 zip codes.

Can someone point me in the right direction of where to learn this functionality quickly? I assume I just need to type those zips into some array or something like that and then I have the submit action check for a match or not. I understand the logic but I have no idea where to begin with the code as I am currently just a html/css static webpage guy.

Thanks alot in advance.
Dave

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈下烟灰 2024-10-21 19:36:06

要了解如何正确组装表单,请访问此网站
然后,您可以将邮政编码数组存储在服务器上,并检查提交的代码是否在该数组中。如何执行此操作取决于您拥有的服务器设置。

要进行客户端验证,您可以设置它并签入 JavaScript。它会类似于:

Form:

<input type="text" id="zipCode">
<input type="button" onclick="checkZipcode()">

JavaScript:

var zipcodes = [12345, 54321];
function checkZipcode() {
  var i, validCode = false;
  for(i = 0;i < zipcodes.length; i++) {
    if (zipcodes[i] == document.getElementById('zipCode').value) {
          validCode = true;
      }
  }

但是,永远不要仅仅依赖客户端验证。确保还执行服务器端验证。

To learn how to assemble your form correctly check out this web site.
Then you can have your array of zip codes on the server and check if the submitted code is in that array. How you do that will depend on what server setup you have.

To do client-side validation, you could set it up and check in the JavaScript. It would be something like:

Form:

<input type="text" id="zipCode">
<input type="button" onclick="checkZipcode()">

JavaScript:

var zipcodes = [12345, 54321];
function checkZipcode() {
  var i, validCode = false;
  for(i = 0;i < zipcodes.length; i++) {
    if (zipcodes[i] == document.getElementById('zipCode').value) {
          validCode = true;
      }
  }

However, never rely solely on client-side validation. Make sure to also perform server-side validation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文