+ Start a Discussion
❤Code❤Code 

How to validate US zip code using jquery

Hi All,

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.
Can anyone help me with this.
 
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>My Page</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css">
    <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="https://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.0/jquery.validate.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.0/jquery.validate.js"></script>
    <style>
    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">
        <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>

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

Regards