简体   繁体   中英

Small Problem with Regular Expression when checking for character input

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.

你应该用

var illegalChars = /[&*%]/ 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM