I am trying to validate a string, that should contain letters numbers and special characters &-._
only. For that I tried with a regular expression.
var pattern = /[a-zA-Z0-9&_\.-]/
var qry = 'abc&*';
if(qry.match(pattern)) {
alert('valid');
}
else{
alert('invalid');
}
While using the above code, the string abc&*
is valid. But my requirement is to show this invalid. ie Whenever a character other than a letter, a number or special characters &-._
comes, the string should evaluate as invalid. How can I do that with a regex?
^
at the beginning and+$
at the end, as the answers below have explained./^[a-zA-Z0-9&_\.-]+$/
. I'm pointing this out in case you missed that subtle difference. – Stifling