Jquery: Form validation not working

前端 未结 2 1905
星月不相逢
星月不相逢 2021-02-06 09:50

I am very new to Jquery and hope you guys can help me with this jquery validation problem.

Been trying to validate the form but it does not validate at all. It accepts a

2条回答
  •  广开言路
    2021-02-06 09:59

    You have 2 elements with the id="form", so the validator is assigned to the div not to the form.

    Change the id value of the div to something else.

    Also the additional-methods.js file must be added after query.validate.js and there is a syntax error because of the alert() within the validate so

    
    
    
    

    then

    jQuery(function ($) {
        $('#form').validate({
            rules: {
                name: {
                    required: true,
                    minlength: 2,
                    maxlength: 20,
                    lettersonly: true
                },
                ssn: {
                    required: true,
                    minlength: 9,
                    maxlength: 9,
                    nowhitespace: true
                },
                gender: {
                    required: true
                },
                mobile: {
                    required: true,
                    minlength: 10,
                    maxlength: 13,
                    digits: true
                },
                address: {
                    required: true,
                    minlength: 10,
                },
                email: {
                    required: true,
                    minlength: 6,
                    email: true
                }
            },
            messages: {
                name: {
                    required: "Please enter your name",
                    minlength: "Name should be more than 2 characters",
                    maxlength: "Name should be less than 20 characters",
                    lettersonly: "Name should contain only letters"
                },
                ssn: {
                    required: "Please enter your NRIC",
                    minlength: "NRIC should be more than 9 characters",
                    maxlength: "NRIC should be less than 9 characters",
                    nowhitespace: "NRIC should not have any spaces"
                },
                gender: {
                    required: "Please select your gender",
                },
                mobile: {
                    required: "Please enter your mobile number",
                    minlength: "Mobile number should be more than 10 characters",
                    maxlength: "Mobile number should be less than 13 characters",
                    digits: "Mobile number should contain only digits"
                },
                address: {
                    required: "Please enter your address",
                    minlength: "Address should be more than 10 characters",
                },
                email: {
                    required: "Please enter your email address",
                    minlength: "Password should be more than 6 characters",
                    email: "Please enter a valid email address"
                }
            },
        });
    });
    

    Demo: Fiddle

提交回复
热议问题