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

前端 未结 3 1887
清歌不尽
清歌不尽 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:02

    Take the Gmail regex from this answer and use it within the addMethod method to create a new custom rule called "gmail"...

    jQuery.validator.addMethod('gmail', function (value, element) {
        return this.optional(element) || /^[a-z0-9](\.?[a-z0-9]){5,}@gmail\.com$/i.test(value);
    }, "not a valid Gmail email address");
    
    $("#myForm").validate({
        rules: {
            myemail: {
                required: true,
                gmail: true
            }
        }
    });
    

    It does not matter if you call .validate() before or after the .addMethod() method.

    DEMO: http://jsfiddle.net/fPFpF/

提交回复
热议问题