html-form: email not validating against pattern

Viewed 87

I'm desperate. Trying to validate an email-form-field with pattern and it don't work.

This is the field:

<form id="benutzerform" name="benutzerform" action="" method="POST" class="row g-3 mb-3 pb-3 needs-validation" novalidate="true">
....
<div class="input-group has-validation">
    <span class="input-group-text"><i class="bi bi-at"></i></span>
    <input type="email" class="form-control" id="email" pattern="/[^@]+@[^@]+\.[a-zA-Z]{2,}/i" required>
    <div class="invalid-feedback"> Eine korrekte Emailadresse ist unbedingt erforderlich!  </div>
</div>

It always shows the invalid-feedback.

When in console I do this, it works.

mailpattern=/^([\w\.\-]+)@([\w\-]+)((\.(\w){2,4})+)$/i;
mailpattern.test('me@home.de');
true

but validating it with this function fails:

function validateFormFields()
{
    let f = document.getElementsByClassName('needs-validation')[0];
    if (f.checkValidity() === false) 
    {
        f.classList.add('was-validated');
        return false;
    }
    return true;
}

Where am I going wrong?

S O L U T I O N

Both ways (@Developer and @Ryszard Czech) lead to validating the form. But the easiest and fastest way is to remove the slashes. Then checkValidity() works like it should!

2 Answers

Use

pattern="[^@]+@[^@]+\.[a-zA-Z]{2,}"

Remove slashes and flag, [a-zA-Z] is case insensitive by itself.

If you need to use the second expression use

pattern="[\w.-]+@[\w.-]+\.\w{2,4}"

Is this what you needed? The main 2 problems were that

  1. you didn't have any element with the class with "needs validation" on it, (I added to your function to check if there are no "needs-validation" classes), in order to prevent errors.
  2. the regex you posted was not applied to the code, and the Html f.checkValididy() function didn't seem to be working, so I replaced the Html validation with your regex.

Here's the Snippet:

<html>

<head>

</head>

<body>
    <div class="input-group has-validation">
        <span class="input-group-text"><i class="bi bi-at"></i></span>
        <input type="email" class="form-control needs-validation" id="email" pattern="/[^@]+@[^@]+\.[a-zA-Z]{2,}/i" required>
        <div class="invalid-feedback"> Eine korrekte Emailadresse ist unbedingt erforderlich! </div>
        <button onclick="alert(validateFormFields());">validate!</button>
    </div>
</body>
<script>
    function validateFormFields() {
        if (document.getElementsByClassName('needs-validation').length > 0) {
            let f = document.getElementsByClassName('needs-validation')[0];
            if (emailAdressIsValid(f.value) === false) {
                f.classList.add('was-validated');
                f.classList.remove('needs-validation');
                return false;
            }
        }
        return true;
    }

    function emailAdressIsValid(email) {
        mailpattern = /^([\w\.\-]+)@([\w\-]+)((\.(\w){2,4})+)$/i;
        return mailpattern.test(email);
    }
</script>

</html>

Related