30

I have JavaScript code to check if special characters are in a string. The code works fine in Firefox, but not in Chrome. In Chrome, even if the string does not contain special characters, it says it contains special characters.

var iChars = "~`!#$%^&*+=-[]\\\';,/{}|\":<>?";

for (var i = 0; i < chkfile.value.length; i++)
{
  if (iChars.indexOf(chkfile.value.charAt(i)) != -1)
  {
     alert ("File name has special characters ~`!#$%^&*+=-[]\\\';,/{}|\":<>? \nThese are not allowed\n");
     return false;
  }
}

Suppose I want to upload a file desktop.zip from any Linux/Windows machine. The value of chkfile.value is desktop.zip in Firefox, but in Chrome the value of chkfile.value is c://fakepath/desktop.zip. How do I get rid of c://fakepath/ from chkfile.value?

fin444
  • 725
  • 2
  • 14
  • 27
ankit
  • 363
  • 2
  • 7
  • 16

6 Answers6

52

You can test a string using this regular expression:

function isValid(str){
 return !/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(str);
}
KooiInc
  • 119,216
  • 31
  • 141
  • 177
16

Try This one.

function containsSpecialCharacters(str){
    var regex = /[ !@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/g;
 return regex.test(str);
}
Vishal Patidar
  • 191
  • 2
  • 9
  • 8
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Donald Duck Feb 13 '17 at 12:25
13

Directly from the w3schools website:

   var str = "The best things in life are free";
   var patt = new RegExp("e");
   var res = patt.test(str);

To combine their example with a regular expression, you could do the following:

function checkUserName() {
    var username = document.getElementsByName("username").value;
    var pattern = new RegExp(/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/); //unacceptable chars
    if (pattern.test(username)) {
        alert("Please only use standard alphanumerics");
        return false;
    }
    return true; //good user input
}
Koffy
  • 768
  • 8
  • 19
5

Did you write return true somewhere? You should have written it, otherwise function returns nothing and program may think that it's false, too.

function isValid(str) {
    var iChars = "~`!#$%^&*+=-[]\\\';,/{}|\":<>?";

    for (var i = 0; i < str.length; i++) {
       if (iChars.indexOf(str.charAt(i)) != -1) {
           alert ("File name has special characters ~`!#$%^&*+=-[]\\\';,/{}|\":<>? \nThese are not allowed\n");
           return false;
       }
    }
    return true;
}

I tried this in my chrome console and it worked well.

sedran
  • 3,498
  • 3
  • 23
  • 39
  • actually problem is whenever i am browsing a file from local machine via chrome it's taking path like c:/ggg/filename but in mozila it takes only filename chkfile.value shoulnot contain full path – ankit Aug 10 '12 at 07:24
  • We should also include tab, backspace.. You can find more special characters at [here](https://msdn.microsoft.com/library/2yfce773(v=vs.94).aspx) – AR M Sep 02 '15 at 14:53
2

You could also do it this way.

specialRegex = /[^A-Z a-z0-9]/ specialRegex.test('test!') // evaluates to true Because if its not a capital letter, lowercase letter, number, or space, it could only be a special character

  • This has the drawback that it does not specify which characters are "special characters". For example, this code considers the character á a special character, although in many contexts, it might not be (for example while checking some French text). So, depending on the context, your solution might be wrong. – liakoyras Sep 20 '19 at 18:14
1

If you don't want to include any special character, then try this much simple way for checking special characters using RegExp \W Metacharacter.

var iChars = "~`!#$%^&*+=-[]\\\';,/{}|\":<>?";
if(!(iChars.match(/\W/g)) == "") {
    alert ("File name has special characters ~`!#$%^&*+=-[]\\\';,/{}|\":<>? \nThese are not allowed\n");
    return false;
}
Chaitanya
  • 719
  • 9
  • 23