1. 程式人生 > >表單驗證-Validform中驗證ip是否存在

表單驗證-Validform中驗證ip是否存在

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來解析