指间宝文档说明

java:

 

m = _c.getJSONObject("pwd");
        //t.put("field", "pwd1");//须与pwd1字段相同
        //t.put("message", "两次密码不一致");
        m.put("identical", t);
        t.put("field", "uid");//不能和uid字段相同
        t.put("message", "不能和用户名相同");
        m.put("different", t);

        t.clear();
        t.put("min", 6);
        t.put("max", m.get("size"));
        m.put("stringLength", t);
        uc.put("pwd", u.toChkHtmlAttr(m));

结果 uc.get("pwd")

 

cnname='密码' isnull='false' regEx='[\s\S]{1,32}' regMsg='密码输入格式不正确,必须填写,且长度不能超过32个字符' identical='{}' different='{"field":"uid","message":"不能和用户名相同"}' stringLength='{"min":6,"max":32}' 

 

html:

// 通过 ${uc.atform}获取表单提交的前缀

 

<form class="cmxform" id="signupForm" atform="${uc.atform}" method="post" action="login.do">


//通过 ${uc.pwd}获取pwd的验证消息

 

 

<input ${uc.pwd} name="pwd" class="form-control" type="password" placeholder="请输入密码">


获取结果如下:

 

 

 

<input cnname="密码" isnull="false" regex="[\s\S]{1,32}" regmsg="密码输入格式不正确,必须填写,且长度不能超过32个字符" identical="{}" different="{"field":"uid","message":"不能和用户名相同"}" stringlength="{"min":6,"max":32}" name="pwd" class="form-control" type="password" placeholder="请输入密码" data-bv-field="pwd">

 

 

 

js:

 

//初始化bootstrapValidator验证器
//qsel form容器(el,JQobj,id,class)
//subfn 提交函数
//返回 bootstrapValidator对象
function initVtor(qsel,subfn){
	var qp=(qsel instanceof jQuery)?qsel:$(qsel);
	if(!qp[0]) return null;
	var sfn=$.isFunction(subfn)?
			subfn:function(qform,vtor,evt){vtor.defaultSubmit()},
		at=qp.attr("atform"),els={};
	qp[0].atform=at?at+".":"";
	$.each(qp[0].elements,
		function(i,el){
			if(!el.name) return;
			var qe=$(el), v={}, js,
				ctag=qe.attr("cnname"),
				rs=qe.parent().find("label[for="+el.name+"]").text();
			ctag=rs?rs:(ctag?ctag:"");
			rs=$.trim(qe.attr("isnull")+"");
			if(rs.length>0 && rs=="false"){
				v["notEmpty"]={
					message:ctag+"不能为空, 请填写"
				};
			}
			rs=$.trim(qe.attr("regEx")+"");
			if(rs!="undefined" && rs.length>0){
				v["regexp"]={
					regexp:$.trim(qe.attr("regEx")),
					message:qe.attr("regMsg")
				};
			}
			rs=$.trim(qe.attr("stringLength")+"");
			if(rs!="undefined" && rs.length>2){
				js=JSON.parse(rs);
				if(js){
					var a=[];
					if(js.min) a.push("不能小于"+js.min+"位");
					if(js.max) a.push("不能大于"+js.max+"位");
					js.message=ctag+a.join(", ");
					v["stringLength"]=js;
				}
			}
			rs=$.trim(qe.attr("identical")+"");
			if(rs!="undefined" && rs.length>2){
				js=JSON.parse(rs);
				if(js) v["identical"]=js;
			}
			rs=$.trim(qe.attr("different")+"");
			if(rs!="undefined" && rs.length>2){
				js=JSON.parse(rs);
				if(js) v["different"]=js;
			}
			if(!$.isEmptyObject(v))
				els[el.name]={validators:v}
	});
	var vtor=qp.bootstrapValidator({
		message:"该字段不能为空, 请填写",
		feedbackIcons: {
          valid: 'glyphicon glyphicon-ok',
          invalid: 'glyphicon glyphicon-remove',
          validating: 'glyphicon glyphicon-refresh'
        },
        //submitHandler:sfn,//版本号v0.5-dev以后不再支持submitHandler配置
        fields:els    
	}).on("success.form.bv",function(ev){
		ev.preventDefault();
		var qform = $(ev.target);
		var vtor = qform.data('bootstrapValidator');
		sfn(qform,vtor,ev);
	});
	//console.dir(els);
	return vtor.data('bootstrapValidator');
}
var fVtor=initVtor("#signupForm",
            	function(qform,validator,evt){
                	//console.log(this,f);
                	wtip.hidestr();
                	var d=getjsform(qform),
                		a=qform[0].atform,pm={};//提交前缀
                	$.each(d, function(k,v) {
						//console.log(k,v);
						pm[a+k]=v;
					});
                	$.post(qform.attr("action"),pm,
						function(re){
							if(re.ok){
								alert("登录成功!");
								return;
							}
							wtip.showstr(re.msg);
							//alert(re.msg);
					},"json");
					return false;
                }
            );

 

 

 

 

 

下面试bootstrap-validator原生的验证方法,以上只是与数据库结合统一写在i.js里,不用每张页面在写验证。

 $('#defaultForm').bootstrapValidator({
         message: 'This value is not valid',
         feedbackIcons: {
             valid: 'glyphicon glyphicon-ok',
             invalid: 'glyphicon glyphicon-remove',
             validating: 'glyphicon glyphicon-refresh'
         },
         fields: {            
             pwd: {
                 message: 'The username is not valid',
                 validators: {
                     notEmpty: {
                         message: 'The username is required and cannot be empty'
                     },
                     stringLength: {
                         min: 6,
                         max: 30,
                         message: 'The username must be more than 6 and less than 30 characters long'
                     },
                     regexp: {
                         regexp: /^[a-zA-Z0-9_\.]+$/,
                         message: 'The username can only consist of alphabetical, number, dot and underscore'
                     },
                     remote: {
                         url: 'remote.php',
                         message: 'The username is not available'
                     },
                     different: {
                         field: 'password',
                         message: 'The username and password cannot be the same as each other'
                     }
                 }
             }
            
     });

 

其他的字段验证方法同上。

 

提交表单时,需要请求reg.do,就需要调用Reg.java的action类里的添加方法

 

      long id = 0;
        Map d = qc.getEntityMap();

        try {
            id = CommBase.GZAdd(d, TableType.SHOPS);
        } catch (DaoException ex) {
            msg = fn.dbErrPut(ex);
            return;
        }

 

 

最后要配置struts.xml文件:

<struts>
  <constant name="struts.locale" value="zh_CN" />
  <constant name="struts.i18n.encoding" value="UTF-8" />
  <constant name="struts.action.extension" value="do" />
  <constant name="struts.multipart.maxSize" value="314572800"/>
  <package name="web" namespace="/" extends="json-default">
    <action name="index" class="web.Index">
      <result>index.jsp</result>
    </action>
    <action name="zhuce" class="web.Zhuce">
      <result>zhuce.jsp</result>
    </action>
  
    
    <action name="login" class="action.Login">
      <result type="json">
        <param name="includeProperties">
          ok,msg
        </param>
      </result>           
    </action>

      
        
    <action name="reg" class="action.Reg">
      <result type="json">
        <param name="includeProperties">
          ok,msg
        </param>
      </result>           
    </action>
    
</struts>

 


 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值