zoukankan      html  css  js  c++  java
  • jquery.validate.js中的remote用法

    记录一下:

    日常开发中我们常使用jquery.validate.js来做表单验证,常用的有required(必填)、maxlength(最大长度)等等。

    有的时候会用到验证某个字段是否已经存在,可以使用jquery.validate.js的remote,具体用法如下:

    <script type="text/javascript">
    var
    validate = null; $(document).ready(function(){ validate = $('#testForm').validate({ onkeyup:false, onfocusout: function(element){ if (element.value != null && element.value != "") { $(element).valid(); } }, rules:{ userName:{ required: url:'checkUserNameExist', type:'post', dataType: "json", cache:false, async:false, data:{userName:function(){return $("#userName" } }, messages:{ routeCode:{ required:"用户名不能为空", remote: "该用户名已经存在" } }, errorPlacement: function(error, element) { layer.tips(error.text(), '#'+element.attr("name"), { tips: [1, '#78BA32'] }); } }); });

    /* 数据保存 */
    function updateSave() {

    // 验证数据
    if (!validate.form()) {
    return false;
    }

    ......

    }

     </script>

     

    原理其实就是调用了一次ajax请求:checkUserNameExist,在后台checkUserNameExist方法里面根据传过来的userName查数据,如果查到了返回false,没有查到返回true。即可实现验证用户名是否存在。

    
    
    
  • 相关阅读:
    Maven部署构件至远程仓库
    Maven远程仓库的认证
    Maven远程仓库的配置
    Maven实战系列文章
    使用Maven私服的好处
    使用Mavne生成可以执行的jar文件
    Visual Studio for Mac 简介
    HTTP 2.0与HTTP 1.1区别
    使用Microsoft的IoC框架:Unity来对.NET应用进行解耦
    围绕央行系统升级所产生的常见问题
  • 原文地址:https://www.cnblogs.com/mark8080/p/7451941.html
Copyright © 2011-2022 走看看