开发者

Javascript for Blackberry Bold that allows user to select a country and then the text field will accept the proper formated zip code

开发者 https://www.devze.com 2023-01-18 19:34 出处:网络
This is for a Blackberry browser app for Blackberry Bold 9700 & 9650 running the 5.0 browser. I want the user to select US or Canada from a drop down then based on that selection the following inp

This is for a Blackberry browser app for Blackberry Bold 9700 & 9650 running the 5.0 browser. I want the user to select US or Canada from a drop down then based on that selection the following input text field will allow the proper zip code formatting. Alpha and numeric if Canada is selec开发者_C百科ted in drop down or just numeric if US is selected in drop down.

Any ideas?

Thanks, Travis


Try this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Untitled Page</title>
    <!--<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>-->
</head>
<body>
    <p>
        <label for="country">
            Country:
        </label>
        <select id="country" name="country">
            <option value="usa">USA</option>
            <option value="canada">Canada</option>
        </select>
    </p>
    <p>
        <label for="zip">
            Zip:</label>
        <input type="text" id="zip" name="zip" />
    </p>
    <script type="text/javascript">

        var countrySelect = document.getElementById('country');
        var zipInput = document.getElementById('zip');

        function validateZip(evt) {

            // regex obtained from http://regexlib.com/REDetails.aspx?regexp_id=2494
            var usZipRegex = /^(\d{5}-\d{4}|\d{5}|\d{9})$/;
            var canZipRegex = /^([a-zA-Z]\d[a-zA-Z]( )?\d[a-zA-Z]\d)$/;
            var zipRegex;

            var country = countrySelect.value;

            if (country === 'usa') {
                zipRegex = usZipRegex;
            }
            else if (country === 'canada') {
                zipRegex = canZipRegex;
            }

            var valid = zipRegex.test(zipInput.value);
            alert(valid ? 'Valid' : 'Invalid');
        }

        zipInput.onchange = validateZip;

    </script>
</body>
</html>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号