Skip to content

Commit

Permalink
validation can handle confirming values equal
Browse files Browse the repository at this point in the history
  • Loading branch information
danielruss committed Oct 31, 2023
1 parent 464584b commit 3440327
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
19 changes: 19 additions & 0 deletions replace2.js
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,24 @@ transform.render = async (obj, divId, previousResults = {}) => {
if (moduleParams.soccer instanceof Function)
moduleParams.soccer();
moduleParams.questName = questName;


// add an event listener to validate confirm...
// if the user was lazy and used confirm instead of data-confirm, fix it now
document.querySelectorAll("[confirm]").forEach( (element) => {
element.dataset.confirm = element.getAttribute("confirm")
element.removeAttribute("confirm")
})
document.querySelectorAll("[data-confirm]").forEach( (element) => {
console.log(element.dataset.confirm)
if (!document.getElementById(element.dataset.confirm)) {
console.warn(`... cannot confirm ${element.id}. `)
delete element.dataset.confirm
}
let otherElement = document.getElementById(element.dataset.confirm)
otherElement.dataset.conformationFor=element.id
})

return true;
};

Expand Down Expand Up @@ -1149,6 +1167,7 @@ function unrollLoops(txt) {
txt = txt.replace(res[loopIndx].orig, cleanedText[loopIndx]);
}
txt = txt.replace(/\xa9/g, "\n");

return txt;
}

Expand Down
21 changes: 17 additions & 4 deletions validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,29 +191,29 @@ function validate_text(inputElement) {
if (!/^(?!9|000|666)(\d)\d{2}-?(?!00)\d{2}-?(?!0000)\d{4}(?<!\1{3}-?\1{2}-?\1{4}?)/.test(inputElement.value) ||
["078-05-1120","219-09-9999"].includes(inputElement.value) ) {
validationError(inputElement, "Please enter a valid Social Security Number in this format: 999-99-9999.")
return
} else {
clearValidationError(inputElement)
}
// if you are a SSN, you cannot be a 4-digit SSN and the length is set...
return
}

// validate a 4 digit SSN
if (inputElement.classList.contains("SSNsm")) {
if (!/^(?!0000)\d{4}/.test(inputElement.value)) {
validationError(inputElement, "Please enter the last four digits of a Social Security Number in this format: 9999.")
return
} else {
clearValidationError(inputElement)
}
return
}

// check string length of text response
if ("minlen" in inputElement.dataset || "maxlen" in inputElement.dataset) {
let textLen = inputElement.value.length;
// the user has not entered anything. Dont bark yet...
if (textLen == 0){
clearValidationError()
clearValidationError(inputElement)
return
}

Expand All @@ -239,8 +239,21 @@ function validate_text(inputElement) {
validationError(inputElement, `Entered text is too long (should have at most ${maxLen} characters)`)
return
}
clearValidationError(inputElement)
}

clearValidationError()
let checkConfirmation = "confirm" in inputElement.dataset ||
"conformationFor" in inputElement.dataset
if (checkConfirmation){
let otherId = inputElement.dataset.confirm ?? inputElement.dataset.conformationFor
let otherElement = document.getElementById(otherId)
if (otherElement.value != inputElement.value){
validationError(inputElement,"Values do not match")
validationError(otherElement,"Values do not match")
}else{
clearValidationError(inputElement)
clearValidationError(otherElement)
}
}
}

Expand Down

0 comments on commit 3440327

Please sign in to comment.