Possible Duplicate:
Check variable equality against a list of values
Javascript: Comparing SINGLE Value Against MULTIPLE Values with OR Operands
First of all I am new to javascript side. Please let me know is there any simple way of formating the below code.
if( fileName== 'doc' || fileName=='docx' || fileName=='xls' || fileName=='xlsx' || fileName=='ppt' || fileName=='pdf' ){
Do something
} else{
Do something
}
var SUPPORTED_FILE_EXTENSIONS = ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pdf'];
if(SUPPORTED_FILE_EXTENSIONS.indexOf(fileName) != -1){
//Do Something
} else {
//Do Something Else
}
Array.indexOf() - Note that this also includes information about adding backward compatibility support for older browsers (I.E. IE).
var validTypes = /docx?|xlsx?|ppt|pdf/;
if (fileName.match(validTypes)) {
...
}
Note: the question mark, ?
, in the Regular Expression indicates the previous character is optional so it can match both doc
and docx
the pipe, |
, indicates or
so it will match doc
or xls
, etc.
maybe something like
var filenames = ['doc','docx','xls','xlsx','ppt','pdf'];
if(filenames.indexOf(fileName) >= 0 ) {
// Do something
} else {
// Do something else
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With