Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript JavaScript validates an invalid email

hebrerillo

Active Coder
Hello there!

I have a form that I am validating using the Form Validtion API in JS. I would expect the email address 'hannibal@gmail' to be invalid.

However, the API returnts that it is valid. Here is an example:

HTML:
<!DOCTYPE html>
<html lang="en">
    <head>
    </head>
    <body>
        <form >
            <input type="email" name="email" value="hannibal@gmail" />
        </form>
        <script>
            const email = document.querySelector("input");
            console.log(email.validity.typeMismatch);
        </script>
    </body>
</html>

The console yields a false, meaning that it is not invalid. But I would expect a type mistmatch error.

Am I missing something??

I have seen other implementations where they use a regex, but I would prefer the JavaScript validation API.

Thank you so much!
 
Solution
Hey there.

Technically, that's not an invalid email address, as you could have, for example, user@localhost, and it is a valid email address.

If you want to make sure the domain has a period in it, then you're going to have to use a regex.


Johna is absolutelly right and these even have some uses in devops

javascript is kind of special, it's a big too generalistic really.

You should try using a regex, in your javascript


I've been using this:
Code:
/^((?!\.)[\w\-_.]*[^.])(@\w+)(\.\w+(\.\w+)?[^.\W])$/
on several projects

I know it looks scary if you are new to regex, but it's a life changing tool

you can even use it directly on the html field HTML attribute: pattern - HTML: HyperText Markup Language | MDN...
Hey there.

Technically, that's not an invalid email address, as you could have, for example, user@localhost, and it is a valid email address.

If you want to make sure the domain has a period in it, then you're going to have to use a regex.
 
It could be down to the browser, i know some browser are not as strict in there checks so what i would do is a HTML check and a Java check afterwards, something like this.


Code:
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
    <form id="myForm">
        <input type="email" name="email" value="hannibal@gmail" />
        <button type="submit">Submit</button>
    </form>
    <script>
        const form = document.getElementById('myForm');
        const email = form.querySelector("input[name='email']");

        form.addEventListener('submit', (event) => {
            if (!email.checkValidity()) {
                console.log('Email is invalid:', email.validationMessage);
                event.preventDefault();
            } else {
                console.log('Email is valid');
            }
        });
 
        console.log('Initial validity check:', email.checkValidity());
    </script>
</body>
</html>
 
Hey there.

Technically, that's not an invalid email address, as you could have, for example, user@localhost, and it is a valid email address.

If you want to make sure the domain has a period in it, then you're going to have to use a regex.


Johna is absolutelly right and these even have some uses in devops

javascript is kind of special, it's a big too generalistic really.

You should try using a regex, in your javascript


I've been using this:
Code:
/^((?!\.)[\w\-_.]*[^.])(@\w+)(\.\w+(\.\w+)?[^.\W])$/
on several projects

I know it looks scary if you are new to regex, but it's a life changing tool

you can even use it directly on the html field HTML attribute: pattern - HTML: HyperText Markup Language | MDN

Best regards
 
Solution
Hello everyone!

Yes, I implemented a regex in the HTML input element. Something like this:

HTML:
<input type="email" pattern="[^@\s]+@[^@\s]+\.[^@\s]+" title="Invalid email address" />

This way, the function "checkValidity" on the input will fail in JavaScript if the email is invalid.

thank you all for your help!
 

New Threads

Buy us a coffee!

Back
Top Bottom