here i have a javascript/html code. My code is supposed to read a string of one or more zipcodes from the user and return the first non-valid zipcode. It should ignore spaces. the user can only use the zipcodes 48103, 48104, 48105, 48106, 48197, 48198. anything else should be invalid. My code mostly works except when the user puts in more than one space to separate the zipcodes. so for example if the user enters "48103, space , space, space, 98324" my program should return 98324 but it just returns the space. Can anyone help? Thank you
<html>
<head>
<title>Due 03/30</title>
<script>
function test()
{
var usr = prompt("Enter a string of zip codes separated by spaces: ");
var array = [];
array = usr.split(" ");
//alert (array);
var pattern = /(4810[3-6])|(4819[7-8])/;
var str;
var isBad = false;
var i;
for (i =0; i < array.length; i++)
{
str = array[i];
if (!str.match(pattern) && str != " ")
{
alert ("The zipcode " + str + " is not a valid zipcode!");
isBad = true;
break;
}
}
if (isBad === false)
alert("All zipcodes are valid");
}
</script>
</head>
<body>
<button onClick="test();">Test String</button>
</body>
</html>
str != "". Without the space.