Regular Expressions 101

Community Patterns

Community Library Entry

0

Regular Expression
PCRE2 (PHP >=7.3)

/
^[^#%^&*\][}{=+\\|><\`~]*$
/

Description

Pattern for validating a text area, to allow all characters except those in the regex pattern. An event listener checks entry on keyup, and displays error or success live in the bootstrap 5 HTML.

 if (field.name === "form_message") {
            const re = /[#%^&*\][}{=+\\|><\`~]/;
            if (re.test(field.value)) {
                let msgString = field.value;
                let match = msgString.match(re)
                this.setStatus(field, `Character '${match}' is not allowed`, "error")
            } else {
                this.setStatus(field, null, "success")
              
            }
Submitted by anonymous - 3 years ago