|
3 | 3 | <%- include("../../partials/exampleInfo") %> |
4 | 4 |
|
5 | 5 | <% if (workspaceIdOk) { %> |
6 | | - <form class="eg" action="" method="post" data-busy="form"> |
| 6 | + <form id="uploadForm" class="eg" action="" method="post" data-busy="form"> |
7 | 7 | <% if(example.Forms && example.Forms[0].FormName) { %> |
8 | 8 | <%- example.Forms[0].FormName %> |
9 | 9 | <% } %> |
|
12 | 12 | <label for="documentName"><%= example.Forms[0].Inputs[0].InputName %></label> |
13 | 13 | <input type="text" class="form-control" id="documentName" placeholder="<%= example.Forms[0].Inputs[0].InputPlaceholder %>" |
14 | 14 | name="documentName" required> |
| 15 | + <small id="nameError" class="form-text text-muted validation-error"></small> |
| 16 | + <small id="emailHelp" class="form-text text-muted"><%= locals.manifest.SupportingTexts.HelpingTexts.SpecifyNameWithExtension %></small> |
15 | 17 | </div> |
16 | 18 |
|
17 | 19 | <div class="form-group"> |
18 | 20 | <label for="documentPath"><%= example.Forms[0].Inputs[1].InputName %></label> |
19 | 21 | <input type="file" accept=".pdf" class="form-control" id="documentPath" placeholder="<%= example.Forms[0].Inputs[1].InputPlaceholder %>" |
20 | 22 | name="documentPath" required> |
21 | | - <small id="error" class="form-text text-muted"></small> |
| 23 | + <small id="pathError" class="form-text text-muted validation-error"></small> |
| 24 | + <small id="emailHelp" class="form-text text-muted"><%= documentFolder %></small> |
22 | 25 | </div> |
23 | 26 |
|
24 | 27 | <input type="hidden" name="_csrf" value="<%- csrfToken %>"> |
25 | 28 | <%- include("../../partials/submitButton") %> |
26 | 29 | </form> |
27 | 30 |
|
28 | 31 | <script> |
29 | | - const fileInput = document.getElementById('documentPath'); |
30 | | - const errorEl = document.getElementById('error'); |
| 32 | + const form = document.getElementById('uploadForm'); |
| 33 | + form.addEventListener('submit', function (event) { |
| 34 | + const docNameInput = document.getElementById('documentName'); |
| 35 | + const fileInput = document.getElementById('documentPath'); |
| 36 | + const nameErrorEl = document.getElementById('nameError'); |
| 37 | + const pathErrorEl = document.getElementById('pathError'); |
31 | 38 |
|
32 | | - fileInput.addEventListener('change', (e) => { |
| 39 | + const documentName = docNameInput.value.trim(); |
33 | 40 | const file = fileInput.files[0]; |
34 | 41 |
|
| 42 | + // Validate documentName ends with .pdf |
| 43 | + if (!documentName.toLowerCase().endsWith(".pdf")) { |
| 44 | + nameErrorEl.textContent = 'Document name must contain .pdf extension'; |
| 45 | + event.preventDefault(); |
| 46 | + event.stopImmediatePropagation(); |
| 47 | + } else { |
| 48 | + nameErrorEl.textContent = ''; // clear any previous errors |
| 49 | + } |
| 50 | +
|
35 | 51 | // Check extension |
36 | 52 | const isPdf = file.name.toLowerCase().endsWith('.pdf'); |
37 | 53 | const isPdfMime = file.type === 'application/pdf'; |
38 | 54 |
|
39 | 55 | if (!isPdf || !isPdfMime) { |
40 | 56 | fileInput.value = ''; // reset file input |
41 | | - errorEl.textContent = 'Invalid file type. Please upload a PDF file.'; |
| 57 | + pathErrorEl.textContent = 'Invalid file type. Please upload a PDF file.'; |
| 58 | + event.preventDefault(); |
| 59 | + event.stopImmediatePropagation(); |
42 | 60 | } else { |
43 | | - errorEl.textContent = ''; // clear any previous errors |
| 61 | + pathErrorEl.textContent = ''; // clear any previous errors |
44 | 62 | } |
45 | | - }); |
| 63 | + }, true); |
46 | 64 | </script> |
47 | 65 | <% } else { %> |
48 | 66 | <%- formatString(example.RedirectsToOtherCodeExamples[0].RedirectText, formatString('href="work00{0}"', example.RedirectsToOtherCodeExamples[0].CodeExampleToRedirectTo)) %> |
|
0 commit comments