How can I allow only gmail.com as domain in my email field validating using jquery-validation plugin?

前端 未结 3 1886
清歌不尽
清歌不尽 2020-12-12 05:27

How can I allow only gmail.com as domain in my email field validating using jquery-validation plugin ?

$(\"#myForm\").validate({
rules: {
    myemail: {
             


        
3条回答
  •  天涯浪人
    2020-12-12 06:20

    You can accept gmail like this below, notice I added in for it to ignore .org and .net as well you can keep tweaking your regex to exactly how you want it.

    \w*\@(?!org)(?!net)[g][m][a][i][l]
    

    Edit based on Sparky's : CASE -- Uppercase and 3 letter email name @gmail.com

    $(document).ready(function () {
    
    jQuery.validator.addMethod('gmail', function (value, element) {
        return this.optional(element) || /^[a-z0-9](\.?[a-z0-9]){3,}@[Gg][Mm][Aa][Ii][Ll]\.com$/i.test(value);
    }, "not a valid Gmail email address");
    
    $('#myform').validate({
        rules: {
            field1: {
                required: true,
                gmail: true
            }
        },
        submitHandler: function (form) { // for demo
            alert('valid form');
            return false;
        }
    });
    
    });
    

提交回复
热议问题