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
<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>
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})*
You can use:
var s='12345,45678, 12345';
var m = s.match(/^(?:\d{5},\s?)*\d{5}$/);