Posted By

charmcitycoder on 12/17/08


Tagged

javascript forms validation validate special characters


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

charmcitycoder
kcmr


Validate for Special Characters


 / Published in: JavaScript
 

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

  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  

You need to login to post a comment.