Validate email address textbox using JavaScript

前端 未结 10 848
一个人的身影
一个人的身影 2020-12-02 17:03

I have a requirement to validate an email address entered when a user comes out from the textbox.
I have googled for this but I got form validation JScript; I don\'t wa

10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 17:46

    You should use below regex which have tested all possible email combination

         function validate(email) {
                var reg = "^[a-zA-Z0-9]+(\.[_a-zA-Z0-9]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,15})$";
                //var address = document.getElementById[email].value;
                if (reg.test(email) == false) 
                {
                    alert('Invalid Email Address');
                    return (false);
                }  
         }
    

    Bonus tip if you're using this in Input tag than you can directly add the regex in that tag example

    
    

    Above you can see two attribute required & pattern in

    required make sure it input block have data @time of submit &
    pattern make sure it input tag validate based in pattern(regex) @time of submit

    For more info you can go throw doc

提交回复
热议问题