Require Domain on Email Address jQuery Validation [duplicate]

自作多情 提交于 2019-12-13 13:12:02

问题


I'm using the following code (with jQuery Validation Plugin) to validate an email address:

    $(".schedule-tour-form").validate({
        rules: {    
            Email: {
                required: true,
                email: true
            }       
        },
    });

    <input id="email" name="Email" class="email" type="email" placeholder="name@email.com" />

Problem is it still allows emails without domain suffixes. For example it validates "test@test" How to I require a domain suffix?


回答1:


The official page of plugin, even consider test@test as a valid mail, then I suppose that this is intended. You could create a new rules, with a strict regex pattern, as suggested here.

//custom validation rule
$.validator.addMethod("customemail", 
    function(value, element) {
        return /^([a-zA-Z0-9_.-+])+\@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/.test(value);
    }, 
    "Sorry, I've enabled very strict email validation"
);

Then to your rules add:

rules: {
                    email: {
                        required:  {
                                depends:function(){
                                    $(this).val($.trim($(this).val()));
                                    return true;
                                }   
                            },
                        customemail: true
                    },


来源:https://stackoverflow.com/questions/27529706/require-domain-on-email-address-jquery-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!