开发者

Textbox character validation

开发者 https://www.devze.com 2023-03-22 21:14 出处:网络
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 si

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");

 }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号