Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RegEXP Help 1

Status
Not open for further replies.

Bastien

Programmer
May 29, 2000
1,683
CA
Hi All

Regexp go me confused. I need to filter some characters out of a text box with regexp. I have the follwoing code

Code:
function checkString(s,oElement)
{
  var thisKey = s.value;
  myRegExp = /'&<>"/i;
  
  if (myRegExp.test( thisKey ))
  {
    alert("You have entered an invalid character in the field.\n\nYou are not allowed to use the following characters in the name.\n\n\tless than symbol( < )  \n\tgreater than symbol ( > )  \n\t ampersand ( & ) \n\tsingle quote( ' ) \n\tdouble quote ( \" )\n\nPlease adjust your entry.");
    document.getElementById(oElement).focus();
    clearField(oElement);
  }  
}



called by:


<input type="textbox" name="base_value_1" id="base_value_1" value="something" onkeyup="checkString(this,'base_value_1');">

I can't seem to get the regexp to test properly. Where I am going wrong?

TIA


Bastien

I wish my computer would do what I want it to do,
instead of what I tell it to do...
 
It's not triggering because your regex is looking for a run of characters in this order: '&<>"

So, unless you have all those characters, in that exact order, the regex will never return true.

There's 2 ways to fix this. First, put an or character between each character:
Code:
/'|&|<|>|"/

Second, this would be the preferred method - put brackets around the characters so the regex looks for any of the characters in the set:
Code:
/['&<>"]/

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
I like RegEx questions, they are like a puzzle.
Here is what I got, and it does work (tested):


Code:
  myRegExp = new RegExp("\'|&|<|>|\"");
 
thanks, guys

Bastien

I wish my computer would do what I want it to do,
instead of what I tell it to do...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top