Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Below is my code which checks for -b.doc in my selected files of that particular multiple fileuploader my problem is suppos if i choose
200-c.docx,201-C.docx,202.wma and 203.wma with same order it is saying validformat it is working some times and not working some times.

What I have tried:

i tried above code but it is not working when i do it multiple times
Posted
Updated 6-Sep-17 22:31pm
v4
Comments
Afzaal Ahmad Zeeshan 6-Sep-17 7:14am    
What is the purpose of this? Be more specific so that we can provide a better solution.

Should tried:
if (myfile.indexOf("-c.doc") != -1 || myfile.indexOf("-C.DOC") > != -1)
 
Share this answer
 
Comments
kav@94 7-Sep-17 1:02am    
i had even tried with if (myfile.indexOf("-c.doc") != -1 || myfile.indexOf("-C.DOC") > != -1) but if i choose the files in
below order it is still saying valid format but it should say invalid format
because in the selected files there is an .mp3 file 400-C.doc,401-c.doc,402-C.doc,403.mp3
You test the filename against lowercase and against uppercase, but any mix will fail.
You need to convert the filename to lowercase or uppercase before testing.
 
Share this answer
 
Comments
Patrice T 7-Sep-17 1:38am    
You should learn to Google for such information.
C#
var myfile = chkFile.value.ToLower();
var validExtension = "-c.doc";
label.innerText = myfile.EndsWith(validExtension) ? "Valid Format" : "Invalid Format";


UPDATE: Here is a test to check that the above code works:

C#6+ version
C#
var files = new List<string> { "aaa.mp3", "aaa-c.mp3", "aAa-c.DoC", "aAa-c.doc" };
var validExtension = "-c.doc";
foreach (var file in files)
{
    Console.WriteLine($"{file} is  {(file.ToLower().EndsWith(validExtension) ? "Valid Format" : "Invalid Format")}");
}

Console.WriteLine("-- done --");
Console.ReadKey();


C# 5.0 or older:
C#
var files = new List<string> { "aaa.mp3", "aaa-c.mp3", "aAa-c.DoC", "aAa-c.doc" };
var validExtension = "-c.doc";
foreach (string file in files)
{
    Console.WriteLine(String.Format("{0} is  {1}", file, 
                      file.ToLower().EndsWith(validExtension)
                          ? "Valid Format"
                          : "Invalid Format");
}

Console.WriteLine("-- done --");
Console.ReadKey();

Output:
aaa.mp3 is  Invalid Format
aaa-c.mp3 is  Invalid Format
aAa-c.DoC is  Valid Format
aAa-c.doc is  Valid Format
-- done --
 
Share this answer
 
v5
Comments
kav@94 7-Sep-17 1:48am    
i tried in this way but still its not working

if (myfile.indexOf("-c.doc").toLowerCase() > 0 || myfile.indexOf("-C.DOC").toLowerCase() > 0 || myfile.indexOf("-C.doc").toLowerCase() > 0 || myfile.indexOf("-c.DOC").toLowerCase() > 0 || myfile.indexOf("-c.docx").toLowerCase() > 0 || myfile.indexOf("-C.DOCX").toLowerCase() > 0 || myfile.indexOf("-C.docx").toLowerCase() > 0 || myfile.indexOf("-c.DOCX").toLowerCase() > 0) {
label.innerText = "Valid Format";
}
else {
label.innerText = "Invalid Format";
chkFile.value = "";
}
}
Graeme_Grant 7-Sep-17 2:25am    
Don't tell me what you are doing, read my solution and replace your code.
kav@94 7-Sep-17 2:46am    
i tried your code only but its not working it is still accepting other extensions
Graeme_Grant 7-Sep-17 2:54am    
Have a look at the update to the solution. Works fine. You have not implemented the change correctly. Run the test code in a new console app and you will see the same output as above.
kav@94 7-Sep-17 3:01am    
i am getting error near foreach loop at var as syntax error and also near console.writeline as expected ')' this is javascript funtion not c# code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900