当前位置 : 主页 > 网络编程 > JavaScript >

jquery validate 前台验证代码

来源:互联网 收集:自由互联 发布时间:2021-06-30
gistfile1.txt $( document ).ready( function () { //当选择框为值$( "#signupForm" ).validate( {onkeyup:false,rules: {uname: {required: true,minlength: 4,remote:{url:"http://localhost/oschina/Addons_Project/Addons/index.php/Rbac/Manager/
gistfile1.txt
$( document ).ready( function () {
        //当选择框为值
			$( "#signupForm" ).validate( {
				onkeyup:false,
				rules: {
					uname: {
						required: true,
						minlength: 4,
						remote:{
							url:"http://localhost/oschina/Addons_Project/Addons/index.php/Rbac/Manager/remoteName",
							type:"post",
						}
					},
					password: {
						required: true,
						minlength: 5
					},
					confirm_password: {
						required: true,
						minlength: 5,
						equalTo: "#password"
					},
					agree: "required"
				},
				messages: {
					uname: {
						required: "用户名必须填写",
						minlength: $.validator.format("用户名长度必须大于{0}"),
						remote:$.validator.format("{0}已经被占用")
					},
					password: {
						required: "密码框必须输入",
						minlength:  $.validator.format("密码长度必须大于{0}")
					},
					confirm_password: {
						required: "请输入确认密码",
						minlength: $.validator.format("密码长度必须大于{0}"),
						equalTo: "两次输入密码不一致"
					},
					agree: "请阅读管理条例"
				},
				errorElement: "em",
				errorPlacement: function ( error, element ) {
					// Add the `help-block` class to the error element
					error.addClass( "help-block" );
					//需要判断单选框的情况
					if ( element.prop( "type" ) === "checkbox" ) {
						error.insertAfter( element.parent( "label" ) );
					} else {
						error.insertAfter( element );
					}
				},
				// 未通过验证的表单元素设置高亮
				highlight: function ( element, errorClass, validClass ) {
					$( element ).parents( ".col-sm-5" ).addClass( "has-error" ).removeClass( "has-success" );
				},
				// 说明:与highlight操作相反 
				unhighlight: function (element, errorClass, validClass) {
					$( element ).parents( ".col-sm-5" ).addClass( "has-success" ).removeClass( "has-error" );
				}
			} );
		} );


//附远程验证方法
    /*添加validate()用户名远程唯一验证方法*/
    public function remoteName(){
		if($_POST)
		{
		    // $uname = $_POST['uname'];
		    $uname=I('post.uname');
		    $data=$this->_model->remoteName($uname);
		    if($data)
		    {
		        echo 'false';
		    }else{
		        echo 'true';
		    }
		    exit();
		}
    }
网友评论