Tag Archives: regular Expression

Regular expression for checking special characters in a string or file name

Today I have faced a problem that codeigniter cannot upload a file if the filename name contain special character. I solved the problem by validatiing the file name with javascript.

Suppose you have a form file elament like this

<input type=”file” name=”baseFile” id=”base-file” />

Then i get the file name by jQuery:

var filename = $(‘#base-file’).val();

and check by a function :

alert(‘The file name may have special character. Please rename the file name and try to upload again.’);
return false;

The function is :

// check file name has special character
function checkSpecialCharacter(filename){
var re = new RegExp(“^[^<>%$#%@|]*$”);
var specialchars = re.test(filename);
return specialchars;

Note : if the function return false then the special character exist in the file name. You can add more special character in[^ special_character].

Leave a comment

Posted by on December 30, 2011 in javascript, jQuery


Tags: , ,