You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1.3 KiB
JavaScript
35 lines
1.3 KiB
JavaScript
function validateContainerNumber(input) {
|
|
const containerNumber = input.value.trim();
|
|
const feedbackElement = input.nextElementSibling;
|
|
|
|
if (!containerNumber) {
|
|
input.classList.remove('invalid-container', 'incomplete-container');
|
|
if (feedbackElement) feedbackElement.remove();
|
|
return;
|
|
}
|
|
|
|
if (containerNumber.length > 0 && containerNumber.length < 11) {
|
|
input.classList.remove('invalid-container');
|
|
input.classList.add('incomplete-container');
|
|
if (feedbackElement) feedbackElement.remove();
|
|
return;
|
|
}
|
|
|
|
fetch(`${validateContainerUrl}${containerNumber}/`)
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
input.classList.remove('incomplete-container');
|
|
if (!data.valid) {
|
|
input.classList.add('invalid-container');
|
|
if (!feedbackElement) {
|
|
const feedback = document.createElement('div');
|
|
feedback.className = 'validation-feedback';
|
|
feedback.textContent = 'Invalid container number';
|
|
input.parentNode.insertBefore(feedback, input.nextSibling);
|
|
}
|
|
} else {
|
|
input.classList.remove('invalid-container');
|
|
if (feedbackElement) feedbackElement.remove();
|
|
}
|
|
});
|
|
} |