// 该jquery扩展引自 http://www.ghostsf.com/tools/389.html 方法名是作者博客的命名
$.fn.ghostsf_serialize = function () {
  var a = this.serializeArray();
  var $radio = $('input[type=radio],input[type=checkbox]', this);
  var temp = {};
  $.each($radio, function () {
  if (!temp.hasOwnProperty(this.name)) {
    if ($("input[name='" + this.name + "']:checked").length == 0) {
    temp[this.name] = "";
    a.push({name: this.name, value: ""});
    }
  }
});
return jQuery.param(a);
};

// 粗制滥作的 表单判空
function verify2(valbtnId,valformId) {

  $(valbtnId).attr('disabled',true);
  var reg = /^\s*$/g;
  $(valformId).keyup('change',function valkeyup(){

  //var formdataarray = $(valformId).serialize().split('&'); 未获取到未选中的 checkbox radio

  //使用扩展后 ghostsf_serialize() 方法可以获取
  var formdataarrayall = $(valformId).ghostsf_serialize().split('&');
  var countf = 0;

  for(var i = 0; i < formdataarrayall.length; i++) {

    var labeltype = formdataarrayall[i].substring(0,formdataarrayall[i].indexOf('='));

    if ($('*[name='+labeltype+']').is('select')) {
    //如果是下拉框 触发键盘事件 ?
      if($('select[name='+labeltype+']').children('options:selected')){
        if (!reg.test($('select[name='+labeltype+']:selected').val())) {
        countf++;
        }
      }
    }

    if ($('*[name='+labeltype+']').is('input')) {
      if($('*[name='+labeltype+']').attr('type') == 'text'){
        if (!reg.test($('input[name='+labeltype+']').val())) {
        countf++;
        }
      }
      if ($('*[name='+labeltype+']').attr('type') == 'checkbox') {
        if ( !reg.test($('input[name='+labeltype+']:checked').val()) && $('input[name='+labeltype+']:checked').val()!= undefined) {
        countf++;
        }
      }
      if ($('*[name='+labeltype+']').attr('type') == 'radio') {
        if (!reg.test($('input[name='+labeltype+']:checked').val()) && $('input[name='+labeltype+']:checked').val()!= undefined) {
        countf++;
        }
      }
    }

  }
  valreturn(countf == i ? false : true,valbtnId);
});

$(valformId).change('click',function valchange(){
    var formdataarrayall = $(valformId).ghostsf_serialize().split('&');
    var countf = 0;
  for(var i = 0; i < formdataarrayall.length; i++) {

    var labeltype = formdataarrayall[i].substring(0,formdataarrayall[i].indexOf('='));

    if ($('*[name='+labeltype+']').is('select')) {

      if($('select[name='+labeltype+']').children('options:selected')){
        if (!reg.test($('select[name='+labeltype+']').val())) {
        countf++;
        }
      }
    }

    if ($('*[name='+labeltype+']').is('input')) {
        //alert($('*[name='+labeltype+']').prop('type'));
        if($('*[name='+labeltype+']').attr('type') == 'text'){
          if (!reg.test($('input[name='+labeltype+']').val())) {
          countf++;
          }
        }
      if ($('*[name='+labeltype+']').attr('type') == 'checkbox') {
        if ( !reg.test($('input[name='+labeltype+']:checked').val()) && $('input[name='+labeltype+']:checked').val()!=undefined) {
        countf++;
        }
      }
      if ($('*[name='+labeltype+']').attr('type') == 'radio') {
        if (!reg.test($('input[name='+labeltype+']:checked').val()) && $('input[name='+labeltype+']:checked').val()!=undefined) {
        countf++;
        }
      }
    }
  }
  valreturn(countf == i ? false : true,valbtnId);
});

}

function valreturn(val,valbtnId){
    $(valbtnId).attr('disabled',val);
    return val;
}

jquery的表单验证方法,一个function能不能同时捕捉点击事件和按键事件?能不能再优化下,有代码。的更多相关文章

  1. jquery实现表单验证简单实例

    /* 描述:基于jquery的表单验证插件. */ (function ($) { $.fn.checkForm = function (options) { var root = this; //将 ...

  2. ASP.NET MVC Jquery Validate 表单验证的多种方式

    在我们日常开发过程中,前端的表单验证很重要,如果这块处理不当,会出现很多bug .但是如果处理的好,不仅bug会很少,用户体验也会得到很大的提升.在开发过程中我们可以不借助 JS 库,自己去手写 JS ...

  3. [转]ASP.NET MVC Jquery Validate 表单验证的多种方式介绍

    在我们日常开发过程中,前端的表单验证很重要,如果这块处理不当,会出现很多bug .但是如果处理的好,不仅bug会很少,用户体验也会得到很大的提升.在开发过程中我们可以不借助 JS 库,自己去手写 JS ...

  4. Jquery Validate 表单验证的多种方式

    ASP.NET MVC Jquery Validate 表单验证的多种方式 在我们日常开发过程中,前端的表单验证很重要,如果这块处理不当,会出现很多bug .但是如果处理的好,不仅bug会很少,用户体 ...

  5. 【锋利的jQuery】表单验证插件踩坑

    和前几篇博文提到的一样,由于版本原因,[锋利的jQuery]表单验证插件部分又出现照着敲不出效果的情况. 书中的使用方法: 1. 引入jquery源文件, 2. 引入表单验证插件js文件, 3. 在f ...

  6. jQuery formValidator表单验证插件

    什么是jQuery formValidator? jQuery formValidator表单验证插件是客户端表单验证插件. 在做B/S开发的时候,我们经常涉及到很多表单验证,例如新用户注册,填写个人 ...

  7. jQuery的表单验证

    jQuery的表单验证 直接上代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...

  8. Jquery 实现表单验证,所有验证通过方可提交

    1. [代码]Jquery 实现表单验证,所有验证通过方可提交 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...

  9. jQuery Validate 表单验证插件----自定义一个验证方法

    一.下载依赖包 网盘下载:https://yunpan.cn/cryvgGGAQ3DSW  访问密码 f224 二.引入依赖包 <script src="../../scripts/j ...

随机推荐

  1. vue有关绑定的知识点

    一.v-bind绑定class 1.对象的形式 <div :class="{text-danger:true}"></div> 数组形式绑定class &l ...

  2. C# 笔记——覆盖和重写

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  3. leetcode 之Single Number(13)

    看见这题我的第一反应是用哈希来做,不过更简洁的做法是用异或来处理,只要是偶数个都为0(0和任意数异或仍为数本身). int singleNumber(int A[], int n) { ; ; i & ...

  4. LeetCode解题报告—— Jump Game & Merge Intervals & Permutation Sequence

    1. Jump Game Given an array of non-negative integers, you are initially positioned at the first inde ...

  5. LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings

    1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...

  6. WordPress 一键置顶文章(推荐用SM Sticky Clicky Star)

    在 WordPress入门 之 发布新文章和管理文章 中,倡萌已经简单提到可以在文章编辑界面或者快速编辑界面设置置顶文章,但是如果你想在后台文章列表中添加一键置顶文章的功能,不妨试试 Quick St ...

  7. 183. Customers Who Never Order

    Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL qu ...

  8. CentOS7.5安装下载工具

    ### 视频下载工具 [you-get](https://github.com/soimort/you-get) 和 [youtube-dl](https://github.com/rg3/youtu ...

  9. 轻松学习LINUX系列教程推出

    本系列多媒体教程已完成的博文: 1.轻松学习Linux之入门篇 http://chenguang.blog.51cto.com/350944/449214 2.轻松学习Linux之本地安装系统 (分区 ...

  10. zabbix通过jvm监控tomcat

    说明:zabbix是通过jvm工具监控tomcat,zabbix server通过连接jvm代理服务器获取tomcat的各种参数 zabbix server:192.168.1.31 tomcat服务 ...