Validate for Special Characters


/ Published in: JavaScript
Save to your folder(s)

Always always always backup client side validation with server side validation! =D


Copy this code and paste it in your HTML
  1. function validateSpecialChars(myStringID)
  2. {
  3. // declare which special chars to validate
  4. var illegalChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
  5. var strToSearch = document.getElementById(myStringID).value;
  6.  
  7. for (var i = 0; i < strToSearch.length; i++)
  8. {
  9. if (illegalChars.indexOf(strToSearch.charAt(i)) != -1)
  10. {
  11. alert ("Your search has one of the following special characters:\n" + iChars + "\nThese are not allowed.\nPlease remove them and try again.");
  12. return false;
  13. }
  14. }
  15. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.