Saturday, October 27, 2012

custom jquery validation


<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.js"></script>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/base/jquery-ui.css" type="text/css" rel="Stylesheet" />
    <script type="text/javascript" language="javascript">
           function customvalidation(element) {
            isvalid = true;
            var classname = $(element).attr('class');
            var value = $(element).val();
            if (classname.indexOf("optional") == -1 && value == "") {
                $(element).next("span").text('Field Required');
                isvalid = false;
            }
            else {
                $(element).next("span").text('');
                if (classname.indexOf("phoneno") != -1) {
                    var numpattern = /^\d{8,12}$/;
                    if (value.match(numpattern)) {
                        $(element).next("span").text('');
                    }
                    else {
                        $(element).next("span").text('Invalid  Number');
                        isvalid = false;
                    }
                }
                if (classname.indexOf("email") != -1) {
                    //verify email start
                    var pattern = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
                    if (value.match(pattern)) {
                        $(element).next("span").text('');
                        if (classname.indexOf("emaildb") != -1) {
                            $.ajax({
                                type: "post",
                                url: "Default5.aspx/checkEmail",
                                data: "{emaiId:'" + value + "' }",
                                async: false,
                                contentType: "application/json; charset=utf-8",
                                dataType: "json",
                                success: function (data) {
                                    if (data.d == "2") {
                                        $(element).next("span").text('');
                                    }
                                    else if (data.d == "1") {
                                        $(element).next("span").text('Purchased Already');
                                        isvalid = false;
                                    }
                                    else if (data.d == "0") {
                                        $(element).next("span").text('Not Exist in Bom Tv');
                                        isvalid = false;
                                    }
                                    else if (data.d == "-1") {
                                        $(element).next("span").text('SQL Error');
                                        isvalid = false;
                                    }
                                    else {
                                        $(element).next("span").text('Error');
                                        isvalid = false;
                                    }

                                },
                                error: function () {
                                    alert('Problem');
                                    isvalid = false;
                                }
                            });
                        }
                    }
                    else {
                        $(element).next("span").text('Invalid Email');
                        isvalid = false;
                    }
                    //verify email end
                }
            }
            return isvalid;
        }
        $(document).ready(function () {
            $('#purchasetable').dialog({ autoOpen: false, modal: true, title: "Enter Purchase Details", width: 600, resizable: false });
            $('#userdetails').dialog({ autoOpen: false, modal: true, title: "Enter User Details", width: 700, resizable: false });

            $("#purchasetable :input").blur(function () {
                var element = $(this);
                customvalidation(element);
            });
            $("#userdetails :input").blur(function () {
                var element = $(this);
                customvalidation(element);
            });
            $("#Purchasebtn").click(function () {
                var invalidcount = 0;
                $("#purchasetable :input[type=text]").each(function (index, element) {
                    if (customvalidation(element) == false)
                        invalidcount = invalidcount + 1;
                });
                if (invalidcount == 0) {
                    $('#userdetails').dialog('open');
                    $('.ui-widget-overlay').click(function () {
                        $('#userdetails').dialog('close');
                    });
                }
            });
            $("#PurchaseCompletebtn").click(function () {
                var invalidcount2 = 0;
                $("#userdetails :input[type=text]").each(function (index, element) {
                    if (customvalidation(element) == false)
                        invalidcount2 = invalidcount2 + 1;
                });
                alert(invalidcount2);
                if (invalidcount2 == 0) {
                    $.ajax({
                        type: "post",
                        url: "Default5.aspx/saveUserDetails",
                        data: "{productId:'productxt',userName:'" + $("#nametxt").val() + "',bomEmailId:'" + $("#bomEmailtxt").val() + "',amazonEmailId:'" + $("#amazonEmailtxt").val() + "',phoneNumber:'" + $("#phoneNumbertxt").val() + "',firstName:'" + $("#firstNametxt").val() + "',middleName:'" + $("#middleNametxt").val() + "',lastName:'" + $("#lastNametxt").val() + "',homeName:'" + $("#houseNametxt").val() + "',street:'" + $("#streettxt").val() + "',city:'" + $("#citytxt").val() + "',state:'" + $("#statetxt").val() + "',country:'" + $("#countrytxt").val() + "',zipCode:'" + $("#ziptxt").val() + "',mobile:'" + $("#mobiletxt").val() + "',homePhone:'" + $("#homePhonetxt").val() + "'}",
                        async: false,
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            if (data.d = "1") {
                                alert('Complete Process');
                                $('#purchasetable').dialog('close');
                                $('#userdetails').dialog('close');
                            }
                        },
                        error: function () {
                            alert('Problem');
                        }
                    });
                }
            });
        });
        function purchasepopup() {
            $('#purchasetable').dialog('open');
            $('.ui-widget-overlay').click(function () { $('#purchasetable').dialog('close'); });
        }
        
    </script>

No comments:

Post a Comment