I currently have a PHP/HTML form with input validation using HTML input patterns. To indicate that the entered input is correct, I utilize the CSS pseudo properties user-invalid/user-valid through a 'check_valid' class as shown below:
input.check_valid:user-invalid { border: 4px dotted red; }
input.check_valid:user-valid { border: 4px solid green; }
The issue arises with textareas due to the lack of pattern support. Although I have:
textarea.check_valid:user-invalid { border: 4px dotted red; }
textarea.check_valid:user-valid { border: 4px solid green; }
This only checks the minlength/maxlength of the textarea content and not the actual content itself.
Server-side validation (in PHP) successfully checks the textarea content, but I aim to implement a visual indication - a green/red border similar to the one for input fields when the content matches a specific regex pattern.
I am seeking a JavaScript snippet to emulate the behavior of check_valid:user-invalid/check_valid:user-valid when the user exits the textarea after entering content. This snippet should validate the content against a regex pattern (enabling pattern support for textareas).
Below is my incomplete attempt thus far:
const textArea1 = document.getElementById("textarea1");
textArea1.addEventListener("change", (event) => {
let patternTextArea1 = /^[a-zA-Z0-9:\s,.'&()\/\-]{10,550}$/;
if (// test textarea content against the regex) {
// apply user-valid - green border
} else {
// apply user-invalid - red dotted line
}
});
My JavaScript skills are lacking, as evident from the above code. I would greatly appreciate any assistance in solving this issue.