Sorry for another regex post but i have been trying to get a regex to work to check for unwanted characters in a string like @#?! I have tried
var cityreg=/^[^$%@!]+$/;
but it doesn't seem to work?
You need to assign the regex pattern object to a variable and then use the test method of the regex object to check the input you pass to it.
function check()
{
var text=prompt("Please enter some text","Harry Potter");
var clean=/^[^$%@!]+$/; //Define your regex pattern
if (text!=null && text!=""){
if (clean.test(text)){ //Use your regex pattern to test input and return true or false
alert(text + " is OK.")
}else{
alert(text + " contains an invalid character.")
}
}
}