jquery.validate.js表單驗(yàn)證

創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站建設(shè)、網(wǎng)站制作、繁昌網(wǎng)絡(luò)推廣、成都小程序開發(fā)、繁昌網(wǎng)絡(luò)營銷、繁昌企業(yè)策劃、繁昌品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供繁昌建站搭建服務(wù),24小時(shí)服務(wù)熱線:18982081108,官方網(wǎng)址:chinadenli.net
官方網(wǎng)站:
API:
當(dāng)前版本:1.5.5
需要JQuery版本:1.2.6+, 兼容 1.3.2
script src="../js/jquery.js" type="text/javascript"/script
script src="../js/jquery.validate.js" type="text/javascript"/script
(1)required:true 必輸字段
(2)remote:"check.php" 使用ajax方法調(diào)用check.php驗(yàn)證輸入值
(3)email:true 必須輸入正確格式的電子郵件
(4)url:true 必須輸入正確格式的網(wǎng)址
(5)date:true 必須輸入正確格式的日期
(6)dateISO:true 必須輸入正確格式的日期(ISO),例如:2009-06-23,1998/01/22 只驗(yàn)證格式,不驗(yàn)證有效性
(7)number:true 必須輸入合法的數(shù)字(負(fù)數(shù),小數(shù))
(8)digits:true 必須輸入整數(shù)
(9)creditcard: 必須輸入合法的信用卡號
(10)equalTo:"#field" 輸入值必須和#field相同
(11)accept: 輸入擁有合法后綴名的字符串(上傳文件的后綴)
(12)maxlength:5 輸入長度最多是5的字符串(漢字算一個(gè)字符)
(13)minlength:10 輸入長度最小是10的字符串(漢字算一個(gè)字符)
(14)rangelength:[5,10] 輸入長度必須介于 5 和 10 之間的字符串")(漢字算一個(gè)字符)
(15)range:[5,10] 輸入值必須介于 5 和 10 之間
(16)max:5 輸入值不能大于5
(17)min:10 輸入值不能小于10
例子:自定義密碼驗(yàn)證的規(guī)則
這里提供兩個(gè)牛B的常用的jQuery表單驗(yàn)證插件:
1、實(shí)例講解表單驗(yàn)證插件Validation的應(yīng)用
2、Validetta : 輕量級的表單驗(yàn)證工具
在表單提交前進(jìn)行驗(yàn)證的幾種方式 .
在Django中,為了減輕后臺壓力,可以利用JavaScript在表單提交前對表單數(shù)據(jù)進(jìn)行驗(yàn)證。下面提供了有效的幾種方式(每個(gè).html文件為一種方式)。
formpage1.html
復(fù)制代碼 代碼如下:
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""
html xmlns=""
head
meta http-equiv="Content-Type" content="text/html; charset=utf-8" /
titleExample1/title
script type="text/javascript" src="/Resource/jquery-1.4.1.js"/script
script type="text/javascript"
function jump()
{
//清空表單所有數(shù)據(jù)
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;
}
})
})
/script
/head
body
提交表單前進(jìn)行驗(yàn)證(方法一)
hr width="40%" align="left" /
form id="form1" method="post" action="/DealWithForm1/"
table
tr
tdfirst_name:/td
tdinput name="firstname" type="text" id="firstname" //td
tdlabel id="firstnameLabel"/label/td
/tr
tr
tdlast_name:/td
tdinput name="lastname" type="text" id="lastname" //td
tdlabel id="lastnameLabel"/label/td
/tr
/table
hr width="40%" align="left" /
button type="submit"提交/button
button type="button" onclick="jump();"取消/button
/form
/body
/html
formpage2.html
復(fù)制代碼 代碼如下:
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""
html xmlns=""
head
meta http-equiv="Content-Type" content="text/html; charset=utf-8" /
titleExample2/title
script type="text/javascript" src="/Resource/jquery-1.4.1.js"/script
script type="text/javascript"
function jump()
{
//清空表單所有數(shù)據(jù)
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;
}
/script
/head
body
提交表單前進(jìn)行驗(yàn)證(方法二)
hr width="40%" align="left" /
form id="form1" method="post" action="/DealWithForm1/" onsubmit="return check()"
table
tr
tdfirst_name:/td
tdinput name="firstname" type="text" id="firstname" //td
tdlabel id="firstnameLabel"/label/td
/tr
tr
tdlast_name:/td
tdinput name="lastname" type="text" id="lastname" //td
tdlabel id="lastnameLabel"/label/td
/tr
/table
hr width="40%" align="left" /
button type="submit"提交/button
button type="button" onclick="jump();"取消/button
/form
/body
/html
formpage3.html
復(fù)制代碼 代碼如下:
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""
html xmlns=""
head
meta http-equiv="Content-Type" content="text/html; charset=utf-8" /
titleExample3/title
script type="text/javascript" src="/Resource/jquery-1.4.1.js"/script
script type="text/javascript"
function jump()
{
//清空表單所有數(shù)據(jù)
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)
{
$("#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();
}
}
/script
/head
body
提交表單前進(jìn)行驗(yàn)證(方法三)
hr width="40%" align="left" /
form id="form1" method="post" action="/DealWithForm1/"
table
tr
tdfirst_name:/td
tdinput name="firstname" type="text" id="firstname" //td
tdlabel id="firstnameLabel"/label/td
/tr
tr
tdlast_name:/td
tdinput name="lastname" type="text" id="lastname" //td
tdlabel id="lastnameLabel"/label/td
/tr
/table
hr width="40%" align="left" /
button type="button" onclick="checktosubmit()"提交/button
button type="button" onclick="jump();"取消/button
/form
/body
/html
以下是視圖函數(shù)、URL配置以及相關(guān)設(shè)置
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
views.py
復(fù)制代碼 代碼如下:
#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("htmlbody"+FirstName+" "+LastName+u"! 你提交了表單!/body/html")
return response
else:
response=HttpResponse()
response.write('htmlscript type="text/javascript"alert("firstname或lastname不能為空!");\
window.location="/DealWithForm1"/script/html')
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="htmlbody"+FirstName+" "+LastName+"! 你提交了表單!"+"/body/html"
return HttpResponse(html)
else:
response=HttpResponse()
response.write('htmlscript type="text/javascript"alert("firstname或lastname不能為空!");\
window.location="/DealWithForm2"/script/html')
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('htmlbody'+FirstName+LastName+u'! 你提交了表單!/body/html')
return response
else:
response=HttpResponse()
response.write('htmlscript type="text/javascript"alert("firstname或lastname不能為空!");\
window.location="/DealWithForm3"/script/html')
return response
else:
return render_to_response('formpage3.html')
urls.py
復(fù)制代碼 代碼如下:
from django.conf.urls.defaults import patterns, include, url
import views
from django.conf import settings
urlpatterns = patterns('',
url(r'^Resource/(?Ppath.*)$','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
復(fù)制代碼 代碼如下:
# 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需要進(jìn)行表單驗(yàn)證, 是網(wǎng)頁在提交數(shù)據(jù)的時(shí)候,為了減輕服務(wù)器的活,把能做的都在前端做了。
比如,用戶輸入一個(gè)手機(jī)號碼,如果該手機(jī)號碼格式是錯(cuò)的,比如格式是158125238pp
然后前端人員又沒有對數(shù)據(jù)進(jìn)行驗(yàn)證,然后又提交到服務(wù)器那里去,很顯然這個(gè)手機(jī)號是錯(cuò)的,服務(wù)器存儲這個(gè)手機(jī)號一點(diǎn)用處也沒有、、、
所以需要進(jìn)行表單驗(yàn)證
jquery判斷表單提交內(nèi)容是否為空
按照代碼就能實(shí)現(xiàn)。
簡單代碼如下:
$(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(“對不起,請?zhí)顚憙?nèi)容!”)
$(“input[name='txtcontent']“).focus();
return false
}
if ($(“input[name='txtcontent']“).val().length 150){
alert(“對不起,內(nèi)容超過150個(gè)字符限制!”)
$(“input[name='txtcontent']“).focus();
return false
}})
$(“#t”).keyup(function(){
$(“.inner”).text($(“input[name='txtcontent']“).val());
}).change(function(){
$(“.inner”).text($(“input[name='txtcontent']“).val());
});
});
這里提供兩個(gè)牛B的常用的jQuery表單驗(yàn)證插件:、實(shí)例講解表單驗(yàn)證插件Validation的應(yīng)
標(biāo)題名稱:表單驗(yàn)證jquery,表單驗(yàn)證正則表達(dá)式
分享URL:http://chinadenli.net/article23/dsihjcs.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、網(wǎng)站導(dǎo)航、自適應(yīng)網(wǎng)站、品牌網(wǎng)站制作、云服務(wù)器、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)