0

I have a LWC component in which one of the part i am sending emails , but here i want to check that the emails are exists in real world or not for now i am just doing the format validation in JS that you can find below =>

validateEmail(email) {
        const regexPattern = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/;
        return regexPattern.test(email);
    }

FYI => I do not want to use third party library.

2
  • 4
    If you want to verify an email address exists "in real world", the only thing you can do is to send an email to this address and ask for a reply. There are many false positives, if you just do a DNS lookup or even send the email and wait for an error response. And there can even be false negatives, where the server pretends the email doesn't exist. E.g. see here. One thing is sure: don't even think of sending an email and waiting for a reply all in Javascript. Commented May 17 at 7:05
  • @FelixvanHove Technically, you could use finger, but not all servers will honor it correctly, because of security/privacy concerns.
    – sfdcfox
    Commented May 17 at 15:25

3 Answers 3

1

There isn't a way to verify that email exists using pure JS.

What you could do is basic regex validation as you are doing.

Or use a 3rd party service but you said you do not want to. See this for more info: https://stackoverflow.com/questions/54020497/validate-if-email-address-exists-in-javascript

3
  • If I had to use any 3rd party service what libraries you could suggest. Commented May 17 at 6:46
  • Never used any myself personally so can't give a recommendation from experience unfortunately. On the link I sent and the question linked as duplicated (stackoverflow.com/questions/102605/…). I think there are some examples of what you could use there
    – Liam Gatt
    Commented May 17 at 6:55
  • Using regular expressions to validate emails is generally not recommended, especially without first understanding RFC 5322. Emails are a lot more complicated than even most developers are aware of.
    – sfdcfox
    Commented May 17 at 15:30
2

I would go back to the source on this. What is the problem you are trying to solve? Checking for a valid looking email address solves users entering their name in the wrong field etc. It won't solve a typo. This is why you are asked to enter a password twice, 1 password set, 1x confirmation.

This sounds like the author is trying to solve an operations or process problem with a technical solution. The problem scenario to prevent might be,

"If the user enters a valid looking email, but it is not a valid email address."

Then the process solution for this is to put the content or next action after a validation link sent to the email address entered. Basically break the process up so your current process stops at sending an email. Then resumes after the user confirms their email by clicking a link.

This process is implemented widely anywhere where emails are entered and should be acceptable with most use cases. Hope this helps.

2
  • Well I don't think breaking the process would work in my scenario , because I just want to send the the attached file to the entered email address and doing validation link process would enhance the functionality. Commented May 17 at 12:23
  • I actually tried with different approach which is integrating with third party tool. Commented May 17 at 12:24
1

To property validate emails, you need a much more complicated regular expression, as outlined in this Stack Overflow answer. That said, LWC already has built-in email address validation. Just change the input type to email:

<lightning-input type="email" ...

Note that you can technically try to finger the email to see if it exists, but most servers probably don't implement it, because of security/privacy concerns. This would involve some kind of middleware, since you can't directly finger in JavaScript, as far as I'm aware.

2
  • But the email type lightning input only validates the syntex of the email but not the availability of the email in real world . Is it good approach to integrate with the online available tools @sfdcfox ? Commented May 18 at 4:14
  • 1
    @NewBaySales Trying to validate if an email is available in the real world is nearly impossible. You can try to use finger, but that is often configured to lie in many organizations, or is disabled completely. The most reliable method is a two-step process of sending an email and having the end user open the email to get a link or code.
    – sfdcfox
    Commented May 18 at 14:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .