Jquery.validate.js实现前端表单验证

发布时间:2024-06-04 14:33:02 来源:君肯网

jquery.validate.js表单验证

官方网站: http://bassistance.de/jquery-plugins/jquery-plugin-validation/

API: http://jquery.bassistance.de/api-browser/plugins.html

当前版本:1.5.5

需要JQuery版本:1.2.6+, 兼容 1.3.2

&ltscript src="../js/jquery.js" type="text/javascript"&gt&lt/script&gt

&ltscript src="../js/jquery.validate.js" type="text/javascript"&gt&lt/script&gt

(1)required:true 必输字段

(2)remote:"check.php" 使用ajax方法调用check.php验证输入值

(3)email:true 必须输入正确格式的电子邮件

(4)url:true必须输入正确格式的网址

(5)date:true 必须输入正确格式的日期

(6)dateISO:true必须输入正确格式的日期(ISO),例如:2009-06-23,1998/01/22 只验证格式,不验证有效性

(7)number:true 必须输入合法的数字(负数,小数)

(8)digits:true 必须输入整数

(9)creditcard: 必须输入合法的信用卡号

(10)equalTo:"#field" 输入值必须和#field相同

(11)accept:输入拥有合法后缀名的字符串(上传文件的后缀)

(12)maxlength:5输入长度最多是5的字符串(汉字算一个字符)

(13)minlength:10 输入长度最小是10的字符串(汉字算一个字符)

(14)rangelength:[5,10] 输入长度必须介于 5 和 10 之间的字符串")(汉字算一个字符)

(15)range:[5,10] 输入值必须介于 5 和 10 之间

(16)max:5 输入值不能大于5

(17)min:10 输入值不能小于10

例子:自定义密码验证的规则

jquery.validate不用submit提交,用js提交的,怎么触发验证啊?

在表单提交前进行验证的几种方式 .

在Django中,为了减轻后台压力,可以利用JavaScript在表单提交前对表单数据进行验证。下面提供了有效的几种方式(每个.html文件为一种方式)。

formpage1.html

复制代码 代码如下:

&lt!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt

&lthtml xmlns="http://www.w3.org/1999/xhtml"&gt

&lthead&gt

&ltmeta http-equiv="Content-Type" content="text/htmlcharset=utf-8" /&gt

&lttitle&gtExample1&lt/title&gt

&ltscript type="text/javascript" src="/Resource/jquery-1.4.1.js"&gt&lt/script&gt

&ltscript type="text/javascript"&gt

function jump()

{

//清空表单所有数据

document.getElementById("firstname").value=""

document.getElementById("lastname").value=""

$("#firstnameLabel").text("")

$("#lastnameLabel").text("")

}

$(document).ready(function(){

$("#form1").bind("submit", function(){

var txt_firstname = $.trim($("#firstname").attr("value"))

var txt_lastname = $.trim($("#lastname").attr("value"))

$("#firstnameLabel").text("")

$("#lastnameLabel").text("")

var isSuccess = 1

if(txt_firstname.length == 0)

{

$("#firstnameLabel").text("firstname不能为空!")

$("#firstnameLabel").css({"color":"red"})

isSuccess = 0

}

if(txt_lastname.length == 0)

{

$("#lastnameLabel").text("lastname不能为空!")

$("#lastnameLabel").css({"color":"red"})

isSuccess = 0

}

if(isSuccess == 0)

{

return false

}

})

})

&lt/script&gt

&lt/head&gt

&ltbody&gt

提交表单前进行验证(方法一)

&lthr width="40%" align="left" /&gt

&ltform id="form1" method="post" action="/DealWithForm1/"&gt

&lttable&gt

&lttr&gt

&lttd&gtfirst_name:&lt/td&gt

&lttd&gt&ltinput name="firstname" type="text" id="firstname" /&gt&lt/td&gt

&lttd&gt&ltlabel id="firstnameLabel"&gt&lt/label&gt&lt/td&gt

&lt/tr&gt

&lttr&gt

&lttd&gtlast_name:&lt/td&gt

&lttd&gt&ltinput name="lastname" type="text" id="lastname" /&gt&lt/td&gt

&lttd&gt&ltlabel id="lastnameLabel"&gt&lt/label&gt&lt/td&gt

&lt/tr&gt

&lt/table&gt

&lthr width="40%" align="left" /&gt

&ltbutton type="submit"&gt提交&lt/button&gt

&ltbutton type="button" onclick="jump()"&gt取消&lt/button&gt

&lt/form&gt

&lt/body&gt

&lt/html&gt

formpage2.html

复制代码 代码如下:

&lt!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt

&lthtml xmlns="http://www.w3.org/1999/xhtml"&gt

&lthead&gt

&ltmeta http-equiv="Content-Type" content="text/htmlcharset=utf-8" /&gt

&lttitle&gtExample2&lt/title&gt

&ltscript type="text/javascript" src="/Resource/jquery-1.4.1.js"&gt&lt/script&gt

&ltscript type="text/javascript"&gt

function jump()

{

//清空表单所有数据

document.getElementById("firstname").value=""

document.getElementById("lastname").value=""

$("#firstnameLabel").text("")

$("#lastnameLabel").text("")

}

function check(){

var txt_firstname = $.trim($("#firstname").attr("value"))

var txt_lastname = $.trim($("#lastname").attr("value"))

$("#firstnameLabel").text("")

$("#lastnameLabel").text("")

var isSuccess = 1

if(txt_firstname.length == 0)

{

$("#firstnameLabel").text("firstname不能为空!")

$("#firstnameLabel").css({"color":"red"})

isSuccess = 0

}

if(txt_lastname.length == 0)

{

$("#lastnameLabel").text("lastname不能为空!")

$("#lastnameLabel").css({"color":"red"})

isSuccess = 0

}

if(isSuccess == 0)

{

return false

}

return true

}

&lt/script&gt

&lt/head&gt

&ltbody&gt

提交表单前进行验证(方法二)

&lthr width="40%" align="left" /&gt

&ltform id="form1" method="post" action="/DealWithForm1/" onsubmit="return check()"&gt

&lttable&gt

&lttr&gt

&lttd&gtfirst_name:&lt/td&gt

&lttd&gt&ltinput name="firstname" type="text" id="firstname" /&gt&lt/td&gt

&lttd&gt&ltlabel id="firstnameLabel"&gt&lt/label&gt&lt/td&gt

&lt/tr&gt

&lttr&gt

&lttd&gtlast_name:&lt/td&gt

&lttd&gt&ltinput name="lastname" type="text" id="lastname" /&gt&lt/td&gt

&lttd&gt&ltlabel id="lastnameLabel"&gt&lt/label&gt&lt/td&gt

&lt/tr&gt

&lt/table&gt

&lthr width="40%" align="left" /&gt

&ltbutton type="submit"&gt提交&lt/button&gt

&ltbutton type="button" onclick="jump()"&gt取消&lt/button&gt

&lt/form&gt

&lt/body&gt

&lt/html&gt

formpage3.html

复制代码 代码如下:

&lt!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt

&lthtml xmlns="http://www.w3.org/1999/xhtml"&gt

&lthead&gt

&ltmeta http-equiv="Content-Type" content="text/htmlcharset=utf-8" /&gt

&lttitle&gtExample3&lt/title&gt

&ltscript type="text/javascript" src="/Resource/jquery-1.4.1.js"&gt&lt/script&gt

&ltscript type="text/javascript"&gt

function jump()

{

//清空表单所有数据

document.getElementById("firstname").value=""

document.getElementById("lastname").value=""

$("#firstnameLabel").text("")

$("#lastnameLabel").text("")

}

function checktosubmit(){

var txt_firstname = $.trim($("#firstname").attr("value"))

var txt_lastname = $.trim($("#lastname").attr("value"))

$("#firstnameLabel").text("")

$("#lastnameLabel").text("")

var isSuccess = 1

if(txt_firstname.length == 0)

{

Jquery.validate.js实现前端表单验证

$("#firstnameLabel").text("firstname不能为空!")

$("#firstnameLabel").css({"color":"red"})

isSuccess = 0

}

if(txt_lastname.length == 0)

{

$("#lastnameLabel").text("lastname不能为空!")

$("#lastnameLabel").css({"color":"red"})

isSuccess = 0

}

if(isSuccess == 1)

{

form1.submit()

}

}

&lt/script&gt

&lt/head&gt

&ltbody&gt

提交表单前进行验证(方法三)

&lthr width="40%" align="left" /&gt

&ltform id="form1" method="post" action="/DealWithForm1/"&gt

&lttable&gt

&lttr&gt

&lttd&gtfirst_name:&lt/td&gt

&lttd&gt&ltinput name="firstname" type="text" id="firstname" /&gt&lt/td&gt

&lttd&gt&ltlabel id="firstnameLabel"&gt&lt/label&gt&lt/td&gt

&lt/tr&gt

&lttr&gt

&lttd&gtlast_name:&lt/td&gt

&lttd&gt&ltinput name="lastname" type="text" id="lastname" /&gt&lt/td&gt

&lttd&gt&ltlabel id="lastnameLabel"&gt&lt/label&gt&lt/td&gt

&lt/tr&gt

&lt/table&gt

&lthr width="40%" align="left" /&gt

&ltbutton type="button" onclick="checktosubmit()"&gt提交&lt/button&gt

&ltbutton type="button" onclick="jump()"&gt取消&lt/button&gt

&lt/form&gt

&lt/body&gt

&lt/html&gt

以下是视图函数、URL配置以及相关设置

--------------------------------------------------------------------------------

--------------------------------------------------------------------------------

views.py

复制代码 代码如下:

#coding: utf-8

from django.http import HttpResponse

from django.shortcuts import render_to_response

def DealWithForm1(request):

if request.method=="POST":

FirstName=request.POST.get('firstname','')

LastName=request.POST.get('lastname','')

if FirstName and LastName:

response=HttpResponse()

response.write("&lthtml&gt&ltbody&gt"+FirstName+" "+LastName+u"! 你提交了表单!&lt/body&gt&lt/html&gt")

return response

else:

response=HttpResponse()

response.write('&lthtml&gt&ltscript type="text/javascript"&gtalert("firstname或lastname不能为空!")\

window.location="/DealWithForm1"&lt/script&gt&lt/html&gt')

return response

else:

return render_to_response('formpage1.html')

def DealWithForm2(request):

if request.method=="POST":

FirstName=request.POST.get('firstname','').encode("utf-8")

LastName=request.POST.get('lastname','').encode("utf-8")

if FirstName and LastName:

html="&lthtml&gt&ltbody&gt"+FirstName+" "+LastName+"! 你提交了表单!"+"&lt/body&gt&lt/html&gt"

return HttpResponse(html)

else:

response=HttpResponse()

response.write('&lthtml&gt&ltscript type="text/javascript"&gtalert("firstname或lastname不能为空!")\

window.location="/DealWithForm2"&lt/script&gt&lt/html&gt')

return response

else:

return render_to_response('formpage2.html')

def DealWithForm3(request):

if request.method=="POST":

FirstName=request.POST.get('firstname','')

LastName=request.POST.get('lastname','')

if FirstName and LastName:

response=HttpResponse()

response.write('&lthtml&gt&ltbody&gt'+FirstName+LastName+u'! 你提交了表单!&lt/body&gt&lt/html&gt')

return response

else:

response=HttpResponse()

response.write('&lthtml&gt&ltscript type="text/javascript"&gtalert("firstname或lastname不能为空!")\

window.location="/DealWithForm3"&lt/script&gt&lt/html&gt')

return response

else:

return render_to_response('formpage3.html')

urls.py

复制代码 代码如下:

from django.conf.urls.defaults import patterns, include, url

import views

from django.conf import settings

urlpatterns = patterns('',

url(r'^Resource/(?P&ltpath&gt.*)$','django.views.static.serve',{'document_root':settings.STATIC_RESOURCE}),

url(r'^DealWithForm1','views.DealWithForm1'),

url(r'^DealWithForm2','views.DealWithForm2'),

url(r'^DealWithForm3','views.DealWithForm3'),

)

settings.py

复制代码 代码如下:

# Django settings for CheckFormBeforeSubmit project.

import os

HERE = os.path.abspath(os.path.dirname(__file__))

DEBUG = True

TEMPLATE_DEBUG = DEBUG

...

STATIC_RESOURCE=os.path.join(HERE, "resource")

...

MIDDLEWARE_CLASSES = (

'django.middleware.common.CommonMiddleware',

'django.contrib.sessions.middleware.SessionMiddleware',

'django.middleware.csrf.CsrfViewMiddleware',

'django.contrib.auth.middleware.AuthenticationMiddleware',

'django.contrib.messages.middleware.MessageMiddleware',

'django.middleware.csrf.CsrfResponseMiddleware',

)

ROOT_URLCONF = 'CheckFormBeforeSubmit.urls'

TEMPLATE_DIRS = (

os.path.join(HERE,'template'),

# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".

# Always use forward slashes, even on Windows.

# Don't forget to use absolute paths, not relative paths.

)

jquery验证表单是否为空

用 button.click提交。

举例如下:

$("#form").validate()

$("#btn).click(function(){

 if($("#form").valid()){

  $("#form").submit()

}

})

jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求。

该插件捆绑了一套有用的验证方法,包括 URL 和电子邮件验证,同时提供了一个用来编写用户自定义方法的 API。所有的捆绑方法默认使用英语作为错误信息,且已翻译成其他 37 种语言。

扩展资料

query-validate 插件

基本用法:

1、页面中引入js依赖,因为validate是依赖jquery的需要先引入jquery。

2、表单校验,首先得有一个表单,即form标签,然后由于浏览器是通过name属性来提交表单数据的,所以需要给校验的控件都加上name属性。

rules里每个控件可以给多个验证方式,常用的有:

1、required 必填验证元素。

2、minlength(length) maxlength(length)。

3、rangelength(range)设置最小长度、最大长度和长度范围 [min,max]。

4、min(value) max(value) range(range) 设置最大值、最小值和值的范围。

5、email() 验证电子邮箱格式。

jquery判断表单提交内容是否为空

按照代码就能实现。

简单代码如下:

$(document).ready(function() {

$(“form”).submit(function(){

if ($(“select[name='boardid']“).val() == “”){

alert(“对不起,请选择类别!”)

$(“select[name='boardid']“).focus()

return false

}

if ($(“select[name='boardid']“).val() == “请选择分类”){

alert(“对不起,请选择类别!”)

$(“select[name='boardid']“).focus()

return false

}

if ($(“input[name='txtcontent']“).val() == “”){

alert(“对不起,请填写内容!”)

$(“input[name='txtcontent']“).focus()

return false

}

if ($(“input[name='txtcontent']“).val().length &gt150){

alert(“对不起,内容超过150个字符限制!”)

$(“input[name='txtcontent']“).focus()

return false

}})

$(“#t”).keyup(function(){

$(“.inner”).text($(“input[name='txtcontent']“).val())

}).change(function(){

$(“.inner”).text($(“input[name='txtcontent']“).val())

})

})

以上就是关于Jquery.validate.js实现前端表单验证全部的内容,如果了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

更多相关资讯

jquery.validate.js表单验证 官方网站: http://bassistance.de/jquery-plugins/jque…
查看详情
jquery.validate.js表单验证 官方网站: http://bassistance.de/jquery-plugins/jque…
查看详情
jquery.validate.js表单验证 官方网站: http://bassistance.de/jquery-plugins/jque…
查看详情
相关文章
推荐游戏
风之谷
风之谷
游戏资讯 10.5M
下载
斗罗大陆3
斗罗大陆3
游戏资讯 566.9M
下载
冠军网球
冠军网球
游戏资讯 148.1M
下载
最佳炮手
最佳炮手
游戏资讯 68.1M
下载
如梦下弦月
如梦下弦月
游戏资讯 840.1M
下载
富甲封神传
富甲封神传
游戏资讯 263.0M
下载