diff --git a/examples/test_scanner_api.html b/examples/test_scanner_api.html index be651b6..bde3a22 100644 --- a/examples/test_scanner_api.html +++ b/examples/test_scanner_api.html @@ -230,7 +230,7 @@

BFD9000 Scanner Control

- +
@@ -253,6 +253,10 @@

BFD9000 Scanner Control

+ + + + @@ -264,7 +268,6 @@

BFD9000 Scanner Control

- Please enter a valid Subject ID (B0001-B9999)
@@ -301,6 +304,8 @@

BFD9000 Scanner Control

const API_BASE = "http://localhost:5000"; const DEVICE_ID = "scanner-001"; + const subjectIdPattern = document.getElementById('subjectId').getAttribute('pattern'); + function showStatus(message, type = 'info') { const statusDiv = document.getElementById('status'); statusDiv.textContent = message; @@ -476,12 +481,7 @@

BFD9000 Scanner Control

const months = parseInt(document.getElementById('ageMonths').value) || 0; // Validate Subject ID format and range - if (!subjectId || !subjectId.match(/^B\d{4}$/)) { - return null; - } - - const subjectNumber = parseInt(subjectId.substring(1)); - if (subjectNumber < 1 || subjectNumber > 5887) { + if (subjectIdParseStatus(subjectId)) { return null; } @@ -491,26 +491,35 @@

BFD9000 Scanner Control

return `${subjectId}${imageType}${sex}${yearsStr}y${monthsStr}m.png`; } + // returns null if good, otherwise returns the string for the error message + function subjectIdParseStatus(subjectId) { + if (!subjectId) { + return 'Please enter a subject ID'; + } + if (!subjectId.match(`^${subjectIdPattern}$`)) { + return 'Subject ID must be a capital letter (B, R) followed by 4 digits'; + } + const subjectNumber = parseInt(subjectId.substring(1)); + if (subjectId[0] === 'B') { + if (subjectNumber < 1 || subjectNumber > 5887) { + return 'Subject ID must be between B0001 and B5887'; + } else return null; + } else if (subjectId[0] === 'R') { + if (subjectNumber < 1 || subjectNumber > 99) { + return 'Subject ID must be between R0001 and R0099'; + } else return null; + } + return 'Unrecognized letter! Subject ID must be in the form B0001-B5887 or R0001-R0099'; + } + function updateFilenamePreview() { const subjectId = document.getElementById('subjectId').value; const filename = generateFilename(); const previewElement = document.getElementById('filenamePreview'); - if (!subjectId) { - previewElement.textContent = 'Please enter a Subject ID'; - previewElement.style.color = '#dc3545'; - return; - } - - if (!subjectId.match(/^B\d{4}$/)) { - previewElement.textContent = 'Subject ID must be B followed by 4 digits'; - previewElement.style.color = '#dc3545'; - return; - } - - const subjectNumber = parseInt(subjectId.substring(1)); - if (subjectNumber < 1 || subjectNumber > 5887) { - previewElement.textContent = 'Subject ID must be between B0001 and B5887'; + let errStatus = subjectIdParseStatus(subjectId); + if (errStatus !== null) { + previewElement.textContent = errStatus; previewElement.style.color = '#dc3545'; return; } @@ -526,30 +535,21 @@

BFD9000 Scanner Control

} function validateFilename(filename) { - // Remove .png extension for validation - const baseFilename = filename.replace('.png', ''); - // Regex pattern: ^(?PB\d{4})(?P[A-Z0-9]+)(?P[MFOU])(?P\d{2,3})y(?P\d{2})m$ - const regex = /^(B\d{4})([A-Z0-9]+)([MFOU])(\d{2,3})y(\d{2})m$/; - return regex.test(baseFilename); + const regex = new RegExp(`^(${subjectIdPattern})([A-Z]{1,2})([MF])(\\d{2,3})y(\\d{2})m\.png$`); + const execRes = regex.exec(filename); + if (!execRes) { + return false; + } + return true; } function downloadImage(dataUrl) { const subjectId = document.getElementById('subjectId').value; const filename = generateFilename(); - if (!subjectId) { - showStatus('Please enter a Subject ID', 'error'); - return; - } - - if (!subjectId.match(/^B\d{4}$/)) { - showStatus('Subject ID must be B followed by 4 digits', 'error'); - return; - } - - const subjectNumber = parseInt(subjectId.substring(1)); - if (subjectNumber < 1 || subjectNumber > 5887) { - showStatus('Subject ID must be between B0001 and B5887', 'error'); + const errStatus = subjectIdParseStatus(subjectId); + if (errStatus !== null) { + showStatus(errStatus, 'error'); return; }