ANP
ANP

Reputation: 15607

Textbox character validation

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?

Upvotes: 0

Views: 2522

Answers (4)

Sarath
Sarath

Reputation: 9146

Try this

var str="your_file_name";

if (/^[^`\\\-/\:_{}\*\?\"\<\>\|\.]+(\.[^\\\/\:\*\?\"\<\>\|\.]+)+$/.test(str)) { 

 alert("valid file name");

 }

Upvotes: 0

Ibu
Ibu

Reputation: 43810

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;
}

Upvotes: 0

Shef
Shef

Reputation: 45589

var input = 'test"';
if(/[\/:*?"<>|]/.test(input)){
    alert('Contains a special char');
} else{
    alert("It's clean!");
}

Upvotes: 0

Rost
Rost

Reputation: 3662

// val - is your value
if (/[\/:*?"<>|]/.test(val)) {
    alert('invalid!');
    // ... prevent form from being sent
}

Upvotes: 2

Related Questions