爱问知识人 爱问教育 医院库

如何应用jquery 表单验证插?

首页

如何应用jquery 表单验证插?

如何应用jquery 表单验证插件

提交回答
好评回答
  • 2013-10-31 17:00:40
      一。下载jquery表单验证插件
        地址:http://bassistance。de/jquery-plugins/jquery-plugin-validation/]http://bassistance。de/jquery-plugins/jquery-plugin-validation/     API:http://docs。
      jquery。com/Plugins/Validation]http://docs。jquery。com/Plugins/Validation 二。使用方法 1)首先在页面head引入jquery。js和jquery。vaidate。
      js 2)验证方法 a)在需要验证的表单元素中加class="验证规则",如下: 规则说明:required:true 必填 minlength:最小长度 maxlength:最大长度 rangelength: [3,10] 长度介于 3 和 {1} 之间的字符串 range:[100,1000] 只能是100和 1000 之间的值" min:最小值 max:最大值 email:true 验证邮箱 url:true 验证是否是合法的网址 date:true 验证是否是合法的日期 new Date() 类型格式 dateISO:true 验证是否是合法的日期 年/月/日 或 年-月-日 格式 number:true 验证是否是合法的数字 digits:true 验证是否为整数 creditcard: 验证合法的信用卡号 equalTo:"要匹配的元素" 如:'#cnfpass' , 验证两次输入值是否相同 accept: "gif|png|jpg" 验证是否是合法后缀名的字符串 remote:'url' 远程验证输入的值是否合法 url是返回true验证成功,false验证失败 这些是插件中提供的验证规则,插件中还提供了增加规则的方法,可以来增加一些我们自己规则,方法如: jQuery。
      validator。addMethod( "规则名称" , function (value, element, params) { return this 。optional(element) || 验证value } b)使用jquery。
      validate的validate方法来验证 $(function(){ $('#frm')。validate({ rules:{ username:{ required:true, minlength:4, remote:'check_username。
      php' }, password:{ required:true, minlength:6, equalTo:'#cnfpass' }, cnfpass:{ required:true } }, messages:{ username:{ required:'必填', minlength:'最小长度3', remote:'已存在' }, password:{ required:'必填', minlength:'最小长度6', equalTo:'两次输入密码不一致' }, cnfpass:{ required:'必填' } }, submitHandler:function(form){ form。
      submit(); //注意这个地方不能用$(from)。submit } }) }) 。

    天***

    2013-10-31 17:00:40

其他答案

    2013-10-31 16:58:06
  •   可以使用jQuery的插件–jQuery-validate表单验证插件来进行表单验证
    例子:
    $(“#form”)。validate({
    //验证规则
    rules:{
    author:{
    required:true,//必需非空字段
    minlength:2     //长度至少两字节
    },
    email:{
    required:true,
    email:true     //此字段为邮件地址
    },
    url:{
    required:false,
    url:true    //此字段为网址
    },
    content:{
    required:true,
    minlength:4
    }
    },
    //错误消息
    messages:{
    author: {
    required: ‘用户名必须填写’,
    minlength:  jQuery。
      format(“名称至少{0}两个字节”) }, email: { required: ‘邮箱必须填写,做为联系方式’, email: ‘邮箱格式不正确’ }, content: { required: ‘网址必须填写’, minlength: jQuery。
      format(“名称至少{0}两个字节”) } }, submitHandler: function(form) { //ajax提交表单,需要jQuery。Form插件 $(form)。ajaxSubmit({ dataType:’json’, success:function(res){ if(res。
      success){ $(‘。ajaxsubmit’)。html(‘发表成功!’)。addClass(‘ok’); $(‘textarea’)。val(”); $(‘#comments’)。prepend(res。msg); }else $(‘。
      ajaxsubmit’)。html(res。msg)。addClass(‘fail’); } }); return false; }, errorPlacement:function(error, element) { //放置错误提示消息的Element error。
      appendTo(element。parent()。find(‘b’)); }, errorClass:’fail’,//错误消息样式 highlight: function(element, errorClass) { //黄褪显示错误字段,需要jQuery。
      Highlight插件 $(element)。highlightFade({color:’yellow’,speed:1000,iterator:’exponential’}) } }); 帮到你就给个好评吧。

    时***

    2013-10-31 16:58:06

类似问题

换一换

相关推荐

正在加载...
最新资料 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):