简体   繁体   中英

Validation Jquery

I have html auth form and validation for it. So, how can I customize a script on that when checking email and the password it checked also symbols (something like preg_match )?

Here is code of javascript:

<script type="text/javascript">
    $(function() {
        // Validation
        $("#sky-form").validate({                   
            // Rules for form validation
            rules:
                {
                    email:
                    {
                        required: true,
                        email: true
                    },
                    password:
                    {
                        required: true,
                        minlength: 3,
                        maxlength: 20
                    }
                },

            // Messages for form validation
            messages:
                {
                    email:
                    {
                        required: 'Please enter your email address',
                        email: 'Please enter a VALID email address'
                    },
                    password:
                    {
                        required: 'Please enter your password'
                    }
                },                  

            // Do not change code below
            errorPlacement: function(error, element)
            {
                 error.insertAfter(element.parent());
            }
        });
    });         
</script>

You can use the custom validation function like below

$.validator.addMethod('methodName', function (value, element, param) {
    // Validation Here

    return isValid; // return true/false
}, 'Error Message');

$('#sky-form').validate({
    rules: {
        email: {
            methodName: true
        }
    }
});

For example purpose suppose you have confirm password field in your form(ie text box with id = confirmPswd )

Now for checking both password same or not you have a custom validation meyhod like below

$.validator.addMethod('validatePassword', function (value, element, param) {
    return value==$("#confirmPswd").val(); 
}, 'Password and Confirm Password should be same');

$('#sky-form').validate({
    rules: {
        password: {
            validatePassword: true
        }
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM