0
$\begingroup$

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

$\endgroup$
5

1 Answer 1

0
$\begingroup$

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");
$\endgroup$
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.