+ Start a Discussion
Herish SurendranHerish Surendran 

How to write regex for email in js of lightning component

Best Answer chosen by Herish Surendran
Deepak_KumarDeepak_Kumar

Hi,

Please add the following pattern in the lighting input type=" email"

pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$"

 If this solves your problem. Mark this as the best answer and solved.
Thanks.
Deepak

All Answers

Deepak_KumarDeepak_Kumar

Hi,

Please add the following pattern in the lighting input type=" email"

pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$"

 If this solves your problem. Mark this as the best answer and solved.
Thanks.
Deepak

This was selected as the best answer
Foram Rana RForam Rana R
Hi Herish,

I hope you are doing well .....!!
Please use the below code I have created Simple visualforce page you can copy and paste in your Org:
<apex:page >
    <script>           
        function checkEmail() {
        alert('@@@Test34');
        var email = document.getElementById('txtEmail');
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        
        if (!filter.test(email.value)) {
            alert('Please provide a valid email address');
            email.focus;
            return false;
        }
    }
    </script>
    
    <apex:form>
        <input type='text' id='txtEmail'/>
        <input type='submit' name='submit' onclick='checkEmail();'/>
    </apex:form>    
</apex:page>


Hope this helps you.
Let me know your review.
If this helps kindly mark it as solved so that it may help others in the future.

Thanks & Regards,
Foram Rana