Click here to Skip to main content
15,512,303 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a array list items called uploadFileList. From the list, FileType is contain only 4 those files called Document1,Document2,Document3,Document4 then it is fine otherwise show not upload FileType name. How can I create validation here?

JavaScript
var uploadFileList = [

{
    "FileId": 0.9451747420274028,
    "FileName": "Document1.pdf",
    "FileType": "Document1",
    "FileLength": "224.87 KB",
    "IsFileUploaded": true
},
{
    "FileId": 0.7652505978317876,
    "FileName": "Document2.pdf",
    "FileType": "Document2",
    "FileLength": "333.36 KB",
    "IsFileUploaded": true
},
{
    "FileId": 0.07227017011947456,
    "FileName": "Document3.pdf",
    "FileType": "Document3",
    "FileLength": "124.54 KB",
    "IsFileUploaded": true
},
{
    "FileId": 0.6777927265002333,
    "FileName": "Document4.pdf",
    "FileType": "Document4",
    "FileLength": "1.00 MB",
    "IsFileUploaded": true
}];


What I have tried:

How to validate and show message for not uploaded files?

JavaScript
if (uploadFileList.FileType.contain("Document1,Document2,Document3")) {
                    alert("Please upload Document4.");
                } else if (uploadFileList.FileType.contain("Document1,Document2,Document4")) {
                    alert("Please upload Document3.");
                }
                else if (uploadFileList.FileType.contain("Document1,Document3,Document4")) {
                    alert("Please upload Document2.");
                }               
Posted
Updated 4-Oct-22 0:39am
v2

1 solution

How about:
JavaScript
const expectedFileTypes = [ "Document1", "Document2", "Document3", "Document4" ];
const fileTypes = uploadFileList.map(f => f.FileType);

const missingFileTypes = expectedFileTypes.filter(t => !fileTypes.includes(t));
if (missingFileTypes.length) {
    alert(`Please upload ${missingFileTypes.join(", ")}.`);
}

const unexpectedFileTypes = fileTypes.filter(t => !expectedFileTypes.includes(t));
if (unexpectedFileTypes.length) {
    alert(`Unsupported file types: ${unexpectedFileTypes.join(", ").`);
}
 
Share this answer
 
Comments
Abhilash.J.A 4-Oct-22 6:13am    
alert showing 'Please upload ${missingFileTypes.join(", ")}.' like this. please check once.
Richard Deeming 4-Oct-22 6:15am    
That suggests you've used the wrong string type. You need to use back-ticks around the strings to indicate they are template literals[^].
Abhilash.J.A 4-Oct-22 6:23am    
okay sir got it. Thank you.

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



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