I have a form where I want the user to only be able to apply alphabetical letters in the City
field, but the input type=text
allows for numbers.
Can anyone help me find a solution to make it so you can only type in alphabetical letters (a-z).
I have tried using RegExp
but it did not work for me. I am using WordPress, maybe it has something to do with it. If there are other solutions prior to regex, I would appreciate it.
<div class="mc-address-fields-group">
<div class="mc-field-group">
<label for="mce-ADDRESS-city">City</label>
<input type="text" maxlength="40" name="CITY" id="mce-ADDRESS-city" class="required" required>
</div>
<div class="mc-field-group">
<label for="mce-ADDRESS-zip">Zip Code</label>
<input type="number" onKeyDown="if(this.value.length==4 && event.keyCode!=8) return false;" name="ZIP" id="mce-ADDRESS-zip" class="required" required>
</div>
</div>
2
Answers
You can use the
pattern
attribute:No sure if you need to add
^
and$
for the start and end as well, but this is the way to prevent numeric characters.You can add a
pattern
attribute to the<input type="text">
elements. This will only work if thetype
istext
.You can start with the following pattern:
"[a-zA-Z][a-zA-Z ]*"
Note: Don’t forget to allow letters AND spaces in a city name. Other characters that should be allowed are hyphens (
-
), dots (.
), and apostrophes ('
) to name a few.Proper zip code validation
FYI, I would not make the zip field a number, as some zip codes are zero-padded. For example, Massachusetts zip codes start with zero.