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?

Recommended Answers

I personally don't like regex, as it takes alot of processing time and it's syntax is very difficult. You can better use a own written function that just simply loops the string, checking each character whether you want it in or not:

function checkString(var string) {

   var …
Jump to Post

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.

Actually, no.

alert(/^[^$%@!]+$/.test(text))

works just as well.

Jump to Post

All 6 Replies

I personally don't like regex, as it takes alot of processing time and it's syntax is very difficult. You can better use a own written function that just simply loops the string, checking each character whether you want it in or not:

function checkString(var string) {

   var stringlength = string.length;
   var i;
   for (i = 0; i < stringlength; i++) {
      if ((string.substr(i, 1)) == "#") {
         alert("Invalid string!");
      }
   }
}

Anyway, for your regex question: see the following page: http://www.evolt.org/article/Regular_Expressions_in_JavaScript/17/36435/

~G

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.")
      }
    }
}

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.

Actually, no.

alert(/^[^$%@!]+$/.test(text))

works just as well.

commented: Good catch. I guess literals are objects too, or at least they have methods. +1

i have been trying to get a regex to work

...but as far as I can tell not reading anything.

Please, rather than continuing to go one-by-one through the character set and posting a question here for each, go to http://www.javascriptkit.com/jsref/regexp.shtml (or one of the dozens of equivalents).

fxm I don't hate regex that word is too short for how much I hate it but one thing I have to say is d5em is a regex genius works perfectly thanks

and you can add charaters to it like {} works great fantastic thanks a g'zillion

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.