Validate email address textbox using JavaScript

前端 未结 10 868
一个人的身影
一个人的身影 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 17:53

    If you wish to allow accent (see RFC 5322) and allow new domain extension like .quebec. use this expression:

    /\b[a-zA-Z0-9\u00C0-\u017F._%+-]+@[a-zA-Z0-9\u00C0-\u017F.-]+\.[a-zA-Z]{2,}\b/
    
    • The '\u00C0-\u017F' part is for alowing accent. We use the unicode range for that.
    • The '{2,}' simply say a minimum of 2 char for the domain extension. You can replace this by '{2,63}' if you dont like the infinitive range.

    Based on this article

    JsFidler

    $(document).ready(function() {
    var input = $('.input_field');
    var result = $('.test_result');
            var regExpression = /\b[a-zA-Z0-9\u00C0-\u017F._%+-]+@[a-zA-Z0-9\u00C0-\u017F.-]+\.[a-zA-Z]{2,}\b/;
        
    			$('.btnTest').click(function(){
              var isValid = regExpression.test(input.val());
              if (isValid)
                  result.html('This email is valid');
               else
                  result.html('This email is not valid');
    
    			});
    
    });
    body {
        padding: 40px;
    }
    
    label {
        font-weight: bold;
    }
    
    input[type=text] {
        width: 20em
    }
    .test_result {
      font-size:4em;
    }
    
    
    
    
    
    Not Tested

提交回复
热议问题