1

I want to validate user name input field by entering special characters:

!`@#$%^&*()+=-[]\\\';,./{}|\":<>?~_

here is my code:

function specialcharecter()
{
    var iChars = "!`@#$%^&*()+=-[]\\\';,./{}|\":<>?~_";   
    var data = document.getElementById("txtCallSign").value;
    for (var i = 0; i < data.length; i++)
    {      
        if (iChars.indexOf(data.charAt(i)) != -1)
        {    
            alert ("Your string has special characters. \nThese are not allowed.");
            document.getElementById("txtCallSign").value = "";
            return false; 
        } 
    }
}
Travesty3
  • 14,351
  • 6
  • 61
  • 98
Manu
  • 59
  • 1
  • 5
  • Please look at the preview before submitting a question. If necessary check the link towards the help that you find in the edit toolbar. – Denys Séguret Mar 18 '13 at 17:57
  • What's the problem with your code ? – Denys Séguret Mar 18 '13 at 17:58
  • @Manu Yes. Whats wrong with your code? – Ezhil V Mar 18 '13 at 18:03
  • Best way would be to go for regEx checking for alphabets and numbers. What if the user enters any other special characters – Ezhil V Mar 18 '13 at 18:04
  • my requiremnt is if if the user enters any other special characters – It should allow him to enter.. – Manu Mar 18 '13 at 18:09
  • As for me - better is to use 'white list' instead of 'black list'. Only in this way you'll be sure that no additional/unexpected chars will be used/saved in DB. E.g. [here](http://stackoverflow.com/questions/1221985/how-to-validate-a-user-name-with-regex) you can check for example regexp. – Andron Mar 18 '13 at 18:30

0 Answers0