Hi. I have a form that I am using for a google map page and I want to limit the input field to allow only a-z, A-Z, 0-9, spaces and hyphens. I was using:
var postCode = $('#form').val();
Validate = /[a-z0-9 A-Z\-]$/.test(postCode);
if (!Validate) {
$('#results').html("Please enter alpha numeric (a-z 0-9) characters");
return;
}
but that doesn’t work. Could someone help me fix it?
I believe you need
/^[a-z0-9 A-Z\-]*$/. Note^in the beginning of the string – together with$at the end it ensures that you validate the whole string and not only its suffix.If you don’t want to accept empty string you can replace
*(zero or more quantifier) with+(one or more) or even specify exact range for length like that:/^[a-z0-9 A-Z\-]{3,10}$/.