Friday, October 26, 2012

Jquery Custom Validation Function


 $("#form1").validate({
                rules: {
                    txtemail: {
                        //start txtemail
                        required: true, email: true,
                        newpattern: function () {
                            var pattern = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
                            var email = $("#emailId").val();
                            if (email.match(pattern)) {
                                $.ajax({
                                    type: "post",
                                    url: "Default2.aspx/checkEmail",
                                    data: "{emaiId:'" + email + "' }",
                                    async: false,
                                    contentType: "application/json; charset=utf-8",
                                    dataType: "json",
                                    success: function (data) {
                                        $("#form1").submit(function () {
                                            if (data.d == 1) {
                                                $.ajax({
                                                    type: "post",
                                                    url: "Default2.aspx/saveUserDetails",
                                                    data: "{emaiId:'abcd1234' }",
                                                    async: false,
                                                    contentType: "application/json; charset=utf-8",
                                                    dataType: "json",
                                                    success: function (data) {
                                                        return true;
                                                    },
                                                    error: function () {
                                                        alert('Problem');
                                                    }
                                                });
                                            }
                                            else
                                                return false;
                                        });
                                    },
                                    error: function () {
                                        alert('Problem');
                                    }
                                });
                            }
                        }
                        //ends txtemail
                    },
                    txtFirstName: {
                        required: true
                    }

                },
                messages: {
                    txtemail: {
                        required: "Email Requrid", email: "Enter a valid Email Addres"
                    },
                    txtFirstName: {
                        required: "First Name Required"
                    }
                }
            });

No comments:

Post a Comment