0

I am using the below code for validating email address using jquery.

I need to validate zipcode using jquery for US only. How can I do it using jquery library.

This is my jQuery code:

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

This is my HTML and 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>

Can anyone help me with this. Regards

Luka Kerr
  • 4,161
  • 7
  • 39
  • 50
user2542953
  • 127
  • 1
  • 9
  • 1
    Possible duplicate of [How to validate US ZIP code using jQuery Validation plugin](http://stackoverflow.com/questions/19011993/how-to-validate-us-zip-code-using-jquery-validation-plugin) – kawadhiya21 Jul 11 '16 at 11:32
  • 1
    Possible duplicate of [ZIP Code (US Postal Code) validation](http://stackoverflow.com/questions/160550/zip-code-us-postal-code-validation) – Turnip Jul 11 '16 at 11:32
  • How to use that one.. Can u explain – user2542953 Jul 11 '16 at 11:37
  • http://stackoverflow.com/questions/160550/zip-code-us-postal-code-validation http://stackoverflow.com/questions/19011993/how-to-validate-us-zip-code-using-jquery-validation-plugin http://stackoverflow.com/questions/15794913/jquery-validation-plugin-accept-only-us-canada-and-mexic-zip-code – Rachel Gallen Jul 11 '16 at 11:57
  • http://jsbin.com/hapoxadehi/edit?html,js,output – Rachel Gallen Jul 11 '16 at 12:11

1 Answers1

0

You most use regex /(^\d{5}$)|(^\d{5}-\d{4}$)/

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