Reputation: 2184
I am validating my text box input using regEx. I have my condition like,"it may take either only characters or both alphanumeric inputs" but it should not take only numbers.
I am using this code for validation:
var regex=/^[0-9]*?[a-zA-Z]+|[a-za-Z]+?[0-9]*$/;
if(!regex.test(city)){
$("#error").html("Please enter a city details Correctly");
}
Its works fine for my condition but the issue is, this even takes spaces between alphanumeric/characters'. How to avoid spaces for my above condition.Please help me to get out from this.
Thanks in advance.
Upvotes: 3
Views: 115
Reputation: 45
Try this,
var regex=/^(?:(?:[0-9]*?[a-zA-Z]+)|(?:[a-zA-Z]+?[0-9]*))$/;
var city = "bangalore east";
if(!regex.test(city)){
alert("Please type valid city");
} else {
alert(city);
}
Upvotes: 1
Reputation: 1548
try out this :
var regex = /^([\d]*?[a-zA-Z]+[a-zA-Z\d]*|[a-zA-Z]+?[a-zA-Z\d]*)$/;
Upvotes: 1
Reputation: 174844
Just put your regex into capturing or non-capturing group, so that it would do an exact line match or otherwise, it would do a partial match like your's.
^([0-9]*?[a-zA-Z]+|[a-zA-Z]+?[0-9]*)$
Upvotes: 2
Reputation: 67988
^(?:(?:[0-9]*?[a-zA-Z]+)|(?:[a-zA-Z]+?[0-9]*))$
Try this.See demo.
http://regex101.com/r/oE6jJ1/30
Upvotes: 1