如何使用 jquery 验证美国邮政编码

How to validate US zip code using jquery

本文关键字:美国 邮政编码 验证 jquery 何使用      更新时间:2023-09-26

我正在使用以下代码通过jquery验证电子邮件地址。

我需要仅使用美国的jquery来验证zipcode。如何使用jquery库来做到这一点。

这是我的jQuery代码:

 $("#page1").on("pageinit", function() {
 $("form").validate({
   rules: {
     email: {
       required: true
     },
     password: {
       required: true
     }
   },
   errorPlacement: function(error, element) {
     error.insertAfter(element.parent());
   }
 });
});

这是我的HTML和CSS:

label.error {
  color: red;
  font-size: 16px;
  font-weight: normal;
  line-height: 1.4;
  margin-top: 0.5em;
  width: 100%;
  float: none;
}
@media screen and (orientation: portrait) {
  label.error {
    margin-left: 0;
    display: block;
  }
}
@media screen and (orientation: landscape) {
  label.error {
    display: inline-block;
    margin-left: 22%;
  }
}
em {
  color: red;
  font-weight: bold;
  padding-right: .25em;
}
</style> </head> <body> <div id="page1" data-role="page"> <div data-role="header">
<body>
  <div id="page1" data-role="page">
    <div data-role="header">
      <h1>Welcome</h1>
    </div>
    
    <div data-role="content">
      <form method="GET">
        
        <div data-role="fieldcontain">
          <label for="email">Email:</label>
          <input type="email" name="email" id="email">
        </div>
        
        <div data-role="fieldcontain">
          <label for="password">Password:</label>
          <input type="password" name="password" id="password">
        </div>
        <div data-role="fieldcontain">
          <label for="zip">Zipcode:</label>
          <input type="text" name="zip" id="zip">
        </div>
        <input data-role="submit" type="submit" value="Login">
      </form>
    </div>
  </div>
</body>

谁能帮我解决这个问题。问候

你最常使用正则表达式/(^'d{5}$)|(^'d{5}-'d{4}$)/

    var isValid=/(^'d{5}$)|(^'d{5}-'d{4}$)/.test(yourvalue);
    if(!isValid) alert("not Valid");