Require domain to email jQuery validation

I am 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" />

      

The problem is that it still allows you to send emails without domain suffixes. For example, it checks for "test @test" How do I require a domain suffix?

+3


source to share


1 answer


plugin's official page , even count as a test@test

valid post, then I guess this is intended. You can create new rules with a strong 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 add to your rules:

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

      

+1


source







All Articles