Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Require Domain on Email Address jQuery Validation [duplicate]

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="[email protected]" />

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

like image 887
Sam Avatar asked Oct 19 '22 20:10

Sam


1 Answers

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
                    },
like image 177
BAD_SEED Avatar answered Oct 22 '22 23:10

BAD_SEED