1. 程式人生 > >前端的驗證框架(jquery-form-validate)

前端的驗證框架(jquery-form-validate)

// jquery-form-validate 前端的驗證框架
        $('.login-form').validate({
            errorElement: 'span', //default input error message container
            errorClass: 'help-block', // default input error message class
            focusInvalid: false, // do not focus the last invalid input
            rules: {
                username: {
                    required: true
                },
                password: {
                    required: true
                }
            },

            messages: {
                username: {
                    required: "使用者名稱不能為空."
                },
                password: {
                    required: "密碼不能為空."
                }
            },

            highlight: function(element) { // hightlight error inputs
                $(element).closest('.form-group').addClass('has-error'); // set error class to the control group
            },

            success: function(label) {
                label.closest('.form-group').removeClass('has-error');
                label.remove();
            },

            errorPlacement: function(error, element) {
                error.insertAfter(element.closest('#input-error'));
            },

            submitHandler: function(form) {
            	
            	var loginForm = $('.login-form');
            	var hdnContextPath = $("#hdnContextPath").val();
            	loginForm.ajaxSubmit({
            		dataType: "json",
                    type: "post", // 提交方式 get/post
                    url: hdnContextPath + '/users/login.action', // 需要提交的 url
                    data: loginForm.serialize(),
                    success: function(data) {
                        // 登入成功或者失敗的提示資訊
                        if (data.status == 200 && data.msg == "OK") {
                        	window.location.href = hdnContextPath + "/center.action";
                        } else {
//                        	SweetAlert.error(data.msg);
                        	alert(data.msg);
                        }
                    }
                });
            	
            }
        });