html:
<form id="formobj">
<input id="hostIp" name="hostIp" type="text" errormsg="ip不存在" datatype="ipValid" />
<span class="Validform_checktip"></span>
</form>
- errormsg是在验证返回false的时候显示的提示信息
- datatype是验证规则,这里的ipValid是自定义的验证规则,平时可以写正则
js:
$('#formobj').Validform({
tiptype:2,
datatype:{
"ipValid":function(gets,obj,curform,regxp){
regxp = new RegExp("^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)($|(?!\.$)\.)){4}$");
if(!regxp.test(gets)) {
return "ip地址格式错误";
}
var result;
$.ajax({
url: "cloudVmController/getHostInfo.do?privateIps="+gets,
contentType: "application/json;charset=utf-8",
type: "post",
dataType: "json",
success: function(r) {
if(!r.success){
result = false;
}else{
result = true;
}
}
});
return result;
}
}
});
- gets表示该input框输入的值(相当于$(“#hostIp”).val())
- obj表示当前input框
- curform表示当前表单(本例为formobj)
- regxp表示正则表达式
- return false表示验证失败 return true表示验证通过
- ajax中success部分的方法要根据后端返回的result来解析