I am taking a textbox value as a file name to store a file and that input comes from the end users. So I need validate that "\/:*?"<>|"
these characters are not present in the input value since file name 开发者_运维知识库can not contain those special character. How I can do this using javascript?
// val - is your value
if (/[\/:*?"<>|]/.test(val)) {
alert('invalid!');
// ... prevent form from being sent
}
var input = 'test"';
if(/[\/:*?"<>|]/.test(input)){
alert('Contains a special char');
} else{
alert("It's clean!");
}
you can use regular expression to test it
var regex = /(\\)|(\/)|(\?)/; // and so on
var input = document.forms[0].files.value;
if (regex.test(input)) {
// the charecter are present;
}
Try this
var str="your_file_name";
if (/^[^`\\\-/\:_{}\*\?\"\<\>\|\.]+(\.[^\\\/\:\*\?\"\<\>\|\.]+)+$/.test(str)) {
alert("valid file name");
}
精彩评论