regex for numbers comma and space

前端 未结 3 856
日久生厌
日久生厌 2021-01-24 08:01

I have a text area and a user can input US zip codes in it separated by a comma or (comma and space).

It could be like 12345,45678, 89654

The following regex i

相关标签:
3条回答
  • 2021-01-24 08:06
    <html>
        <head>
            <title></title>
            <script type="text/javascript">
                function Validate(txt) {
                    txt.value = txt.value.replace(/[^, 0-9]+/g, '');
                }
            </script>
        </head>
        <body>
            <form>
                <input type = "text" id ="txt" onkeyup = "Validate(this)" />
            </form>
        </body>
    </html>
    
    0 讨论(0)
  • 2021-01-24 08:12

    Use this regex: ^\d{5}(, ?\d{5})*$

    It specifies 5 digits at the beginning: ^\d{5} and any number of other comma, space, and 5 digit combinations: (, ?\d{5})*

    0 讨论(0)
  • 2021-01-24 08:32

    You can use:

    var s='12345,45678, 12345';
    var m = s.match(/^(?:\d{5},\s?)*\d{5}$/);
    
    0 讨论(0)
提交回复
热议问题