This is a migrated thread and some comments may be shown as answers.
OnClientFileSelected filename validation
1 Answer 20 Views
This is a migrated thread and some comments may be shown as answers.
Yuly
Top achievements
Rank 1
Yuly asked on 01 Sep 2016, 12:18 PM

Please bear with me, as this is my first post.

I am trying to do a clientside validation on filename expression. I do not want to have :, /, %, etc in my file names. How do you guys do that?

1 Answer, 1 is accepted

Sort by
0
Ivan Danchev
Telerik team
answered on 05 Sep 2016, 12:26 PM
Hello Yuly,

You can prevent the upload of files with names containing special characters, "/%" for example, by subscribing to the AsyncUpload's OnClientFileUploading event and cancelling the event in case the filename contains a forbidden character:
function OnClientFileUploading(sender, args) {
    var fileName = args.get_fileName();
 
    if (!isValid(fileName)) {
        args.set_cancel(true);
    }
}
 
function isValid(str) {
    return !/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(str);
}

The event will fire for every selected file, so in case you are selecting multiple files (MultipleFileSelection="Automatic") the event will be cancelled only for those with special characters in their name. 

Regards,
Ivan Danchev
Telerik by Progress
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Asked by
Yuly
Top achievements
Rank 1
Answers by
Ivan Danchev
Telerik team
Share this question
or