I am trying to validate a file name with invalid characters in the file name without validating file path. I have tried various combinations but none of my javascript functions seem to do the trick.
@Html.TextBoxFor(model => model.Attachment, new { type = "file" , onchange = "ValidateFileName(this)" })
JS:
function ValidateFileName(name) {
if (name.value.match(/\.\.[a-z][a-z][a-z]$/) || name.value.match(/\.\.[a-z][a-z][a-z][a-z]$/) || name.value.match(/^(?!\.)(?!com[0-9]$)(?!con$)(?!lpt[0-9]$)(?!nul$)(?!prn$)[^\|\*\?\\:%<>/$"]*[^\.\|\*\?\\:%<>/$"]+$/)) {
alert('Invalid file extension.');
name.form.reset();
name.focus();
return false;
}
}