views:

30

answers:

1

I have made a simple webpage to check for regular expressions.

<html>
<head>
<script type="text/javascript">  

var illegalChars = /^[&*%]+$/

function validate_form(thisform)
{
with (thisform)
  { 
  if (illegalChars.test(regextest.value)==true)
    alert('Illegal Characters');
  {regextest.focus();return false;}
  }
}
</script>
</head>

<body>
  <h2>RegEx Stuff</h2>
  <form action="regex.html" onsubmit="return validate_form(this)" method="post">
  Enter characters:
  <br/> 
  <input type="text" name="regextest" size="60"> 
  <br/>
  <input type="submit" value="Submit">
  </form>
</body>

</html>

The idea being I want to scan the input string and return false if any of the illegal chars are found - in this case &, * and %.

The illegalChars variable will contain all charcaters I want to avoid being put into a DB.

It works if I put 1 character in the text field otherwise it fails. I want to check for illegal charcaters throughout the string. So something like "Chalk&Cheese" should fail. Also multiples of the illegal characters.

Any suggestions. Cheers.

+4  A: 

You should use

var illegalChars = /[&*%]/ 
Jens
how come what i was doing was wrong if you don't mind me asking.
Uncle
@Dan: because you used anchors for start and end of string. It was trying to match the whole input string.
SilentGhost
Victor Nicollet
thanks for this!
Uncle