Skip to content

Commit deecc24

Browse files
committed
Added state validation
1 parent 7bbfe29 commit deecc24

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/_includes/components/codepens/identify-spec.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@
8282
cityField.style.outline = "";
8383
}
8484

85+
// Regular expression check for city name
86+
var stateRegex = /^[A-Z]{2}$/;
87+
if (!stateRegex.test(state)) {
88+
const stateError = document.getElementById("state-error");
89+
stateError.style.display = "block";
90+
const stateField = document.getElementById("state");
91+
stateField.style.outline = "2px solid red";
92+
errors.push("State is invalid");
93+
} else {
94+
const stateError = document.getElementById("state-error")
95+
stateError.style.display = "none";
96+
const stateField = document.getElementById("state")
97+
stateField.style.outline = "";
98+
}
99+
85100
// Regular expression check for postcode
86101
var postcodeRegex = /^\d{5}$/;
87102
if (!postcodeRegex.test(postcode)) {
@@ -183,6 +198,7 @@ <h4> Sample Identify </h4>
183198
<span class="error-message" id="city-error">Please enter a valid city name in the 'City' field.</span>
184199
<label for="state">State:</label>
185200
<input type="text" id="state" value="TN">
201+
<span class="error-message" id="state-error">Please enter a valid two-letter state code in all caps, like 'CA' for California.</span>
186202
<label for="postcode">Zip Code:</label>
187203
<input type="text" id="postcode" value="37215">
188204
<span class="error-message" id="postcode-error">Please enter a valid five-digit zip code.</span>

0 commit comments

Comments
 (0)