0

am trying to upload file which file name contains only in english character with symbol or number ,my problem is if my file name contains english with chinese character then i need to validate it and need to show alert message that file name should'nt be in non english character

can anybody send me this code

vijay
  • 11
  • 1
  • 3
  • 3
    Why do you care? You should never use user uploaded filenames for anything anyway. The contents of the file are important, not how the user labeled it on his computer. – deceze Jan 25 '11 at 07:28
  • Check this: http://stackoverflow.com/questions/150033/regular-expression-to-match-non-english-characters – Marcin Jan 25 '11 at 07:29
  • client will send one mail with attachment my job is i need to read mail with attachment and i need it to store it in my server, if my attachment contains no english character file name then i need to show alert message – vijay Jan 25 '11 at 07:32
  • Again, why?! I'd find this highly annoying. – deceze Jan 25 '11 at 07:33
  • yes i need to store that attachment first in my folder when i trying to save this file i need to read file name here exactly my code is failing , if i show a error messege that i can allow only english character file name, – vijay Jan 25 '11 at 07:38
  • Don't save the file under the original filename. Never! Make up a new random filename. – deceze Jan 25 '11 at 07:39
  • @deceze: I plan to do the same for a website (i.e. use the original file names on the server, appending a sequential number if duplicate). Wondering why you consider it a bad practice. – Salman A Jan 25 '11 at 07:45
  • @Salman Because the user supplied filename may a) already exist on the server and b) be invalid as a filename. You need to a) check if the name already exists and add numbers (as you say) and b) make sure it's a valid filename and replace/escape invalid characters. So most of the time, you're not using the original filename anyway. Why bother with all the checking and escaping then? Just generate a random unique filename and be done with it. Save the original filename in a database if you really need it later. – deceze Jan 25 '11 at 09:26

4 Answers4

4

Get the value of the file input, and match it against the regex \w

But you should not disallow this. You should instead rename the file after it is uploaded. The user may upload a file with a name valid in his OS, but invalid on the server OS. You can still store the original filename is a database, if needed.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • i need to show error message if they trying to upload file name contain non english character file – vijay Jan 25 '11 at 07:34
  • if(filename != regex[\w]) alert("non english character not allowed"); is it correct – vijay Jan 25 '11 at 07:43
  • @vijay `filename.matches(/\w/g)` – Bozho Jan 25 '11 at 08:01
  • if(fileName.matches(/\w/g)) { alert("english"); } else { alert("non english character"); } its showing error – vijay Jan 25 '11 at 08:08
  • object doesn't support this property or method – vijay Jan 25 '11 at 08:13
  • what is `fileName` ? how did you obtain it. – Bozho Jan 25 '11 at 08:14
  • from acessing this id in my javascript function var fileName = document.getElementById("txtFile").value; and the file name is in english with chinesecharacter file name is Cisco-S11-POA1800005815-Inv04736851-100919重做没有.pdf – vijay Jan 25 '11 at 08:17
  • @vijay - try this on firefox, debug with firebug – Bozho Jan 25 '11 at 08:20
  • @Bozho no my application supports only in internet explorer any it is my comany rule – vijay Jan 25 '11 at 08:23
  • @vijay what a crappy company then ;) seriously. Then use some IE debug tool - I think there were some. Or you can still use firefox for debug purposes. – Bozho Jan 25 '11 at 08:26
  • do u have any idea about this [\u4E00-\u9FFF] can i use this to check if(filename.match[\u4E00-\u9FFF]) – vijay Jan 25 '11 at 08:28
  • var str="Cisco-S11-POA1800005815-Inv04736851-100919重做没有.pdf"; var patt1=[\u4E00-\u9FFF]; if(str.match(patt1)) { alert("filename is english"); } please check this can i use like this – vijay Jan 25 '11 at 08:33
  • any alternate method can u send me all english character symbol small cap regular expression i will check opposite to that – vijay Jan 25 '11 at 08:43
  • `\w` means all english characters. The problem is not in the regex. Debug your code. – Bozho Jan 25 '11 at 08:46
  • if i check with all special character i hope my problem will be solved – vijay Jan 25 '11 at 08:54
0

this could work for non english characters

[^A-Za-z0-9_,.@\(\)\&\+\-\!\#\$\%\^\*\;\\\/\|\<\>'":\?\=\s]+
Vikas Gautam
  • 1,793
  • 22
  • 21
0

get filename with this code:

function checkname(event) {
 var fullPath = document.getElementById('file').value;
  var filename;
 if (fullPath) {
  var startIndex = (fullPath.indexOf('\\') >= 0 ? fullPath.lastIndexOf('\\') : fullPath.lastIndexOf('/'));
  filename = fullPath.substring(startIndex);
  if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
   filename = filename.substring(1);
  }
 }

  if (!/^[a-zA-Z0-9$@$!%*?&#^-_-. +]+$/.test(filename)) {
  console.log("error: "+filename);
  return;
 } else {
    console.log("ok");
  }

};
<form action="" method='post' enctype='multipart/form-data'>
  <input type='file' id='file' name='file' onchange="checkname(event);">
  <input type='submit' value='upload'>
</form>

and use this code for validation:

/^[a-zA-Z0-9$@$!%*?&#^-_. +]+$/.test(filename);

if you want dont allow use characters use this:

 /^[a-zA-Z0-9]+$/.test(filename);
0

Okay, I'm going to come out and say that you should probably just match the filename to your alphanumeric regex, and throw out a generic error if it doesn't match. However, if you are dead set on giving a specific warning for Chinese characters, use the following regex:

[\u4E00-\u9FFF]

A sharp eye may also recognize this as a subset of Japanese (since Japanese calls the Chinese character set 'Kanji').

if(preg_match("[\u4E00-\u9FFF]+", $filename))
  { echo "Chinese characters found in filename."; }
Fibericon
  • 5,684
  • 12
  • 37
  • 64