亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > JavaScript > 正文

在表單提交前進行驗證的幾種方式整理

2019-11-20 22:29:07
字體:
來源:轉載
供稿:網友
在表單提交前進行驗證的幾種方式 .
在Django中,為了減輕后臺壓力,可以利用JavaScript在表單提交前對表單數據進行驗證。下面提供了有效的幾種方式(每個.html文件為一種方式)。
formpage1.html
復制代碼 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example1</title>
<script type="text/javascript" src="/Resource/jquery-1.4.1.js"></script>
<script type="text/javascript">
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;
}
})
})
</script>
</head>
<body>
提交表單前進行驗證(方法一)
<hr width="40%" align="left" />
<form id="form1" method="post" action="/DealWithForm1/">
<table>
<tr>
<td>first_name:</td>
<td><input name="firstname" type="text" id="firstname" /></td>
<td><label id="firstnameLabel"></label></td>
</tr>
<tr>
<td>last_name:</td>
<td><input name="lastname" type="text" id="lastname" /></td>
<td><label 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
復制代碼 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example2</title>
<script type="text/javascript" src="/Resource/jquery-1.4.1.js"></script>
<script type="text/javascript">
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;
}
</script>
</head>
<body>
提交表單前進行驗證(方法二)
<hr width="40%" align="left" />
<form id="form1" method="post" action="/DealWithForm1/" onsubmit="return check()">
<table>
<tr>
<td>first_name:</td>
<td><input name="firstname" type="text" id="firstname" /></td>
<td><label id="firstnameLabel"></label></td>
</tr>
<tr>
<td>last_name:</td>
<td><input name="lastname" type="text" id="lastname" /></td>
<td><label 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
復制代碼 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example3</title>
<script type="text/javascript" src="/Resource/jquery-1.4.1.js"></script>
<script type="text/javascript">
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)
{
$("#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>
提交表單前進行驗證(方法三)
<hr width="40%" align="left" />
<form id="form1" method="post" action="/DealWithForm1/">
<table>
<tr>
<td>first_name:</td>
<td><input name="firstname" type="text" id="firstname" /></td>
<td><label id="firstnameLabel"></label></td>
</tr>
<tr>
<td>last_name:</td>
<td><input name="lastname" type="text" id="lastname" /></td>
<td><label 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>

以下是視圖函數、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("<html><body>"+FirstName+" "+LastName+u"! 你提交了表單!</body></html>")
return response
else:
response=HttpResponse()
response.write('<html><script 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="<html><body>"+FirstName+" "+LastName+"! 你提交了表單!"+"</body></html>"
return HttpResponse(html)
else:
response=HttpResponse()
response.write('<html><script 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('<html><body>'+FirstName+LastName+u'! 你提交了表單!</body></html>')
return response
else:
response=HttpResponse()
response.write('<html><script type="text/javascript">alert("firstname或lastname不能為空!");/
window.location="/DealWithForm3"</script></html>')
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<path>.*)$','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.
)
...
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
精品动漫一区二区三区| 国产精品成人aaaaa网站| 亚洲精品电影在线| 精品久久久久久久久久久久| 午夜精品一区二区三区在线播放| 亚洲视频在线观看网站| 久久精品免费播放| 亚洲一区二区少妇| 亚洲夜晚福利在线观看| 亚洲欧美一区二区三区在线| 欧美日韩亚洲天堂| 精品亚洲夜色av98在线观看| www.午夜精品| 久久国产精品亚洲| 中文字幕综合在线| 日韩高清av在线| 欧美最猛性xxxxx(亚洲精品)| 亚洲桃花岛网站| 日韩精品亚洲元码| 亚洲精品日韩激情在线电影| 黑人巨大精品欧美一区二区一视频| 国产精品美女999| 在线亚洲午夜片av大片| 欧美性猛交xxxx久久久| 亚洲香蕉av在线一区二区三区| 97av在线视频免费播放| 日本aⅴ大伊香蕉精品视频| 日本精品一区二区三区在线| 俺去啦;欧美日韩| 日韩av日韩在线观看| 久久视频国产精品免费视频在线| 亚洲bt欧美bt日本bt| 77777少妇光屁股久久一区| 热99精品里视频精品| 国产欧美一区二区三区久久| 久久久久免费精品国产| 国产日韩在线亚洲字幕中文| 国产精品美女久久久免费| 日韩成人在线网站| 中文字幕欧美视频在线| 日韩精品中文字幕视频在线| 亚洲男人天堂网| 国产精品日韩欧美综合| 国产成人aa精品一区在线播放| 国产精品免费小视频| 亚洲三级 欧美三级| 国产精品jvid在线观看蜜臀| 亚洲精品456在线播放狼人| 少妇高潮久久久久久潘金莲| 国产精品久久久久77777| 国产精品一区二区三区久久久| www.日韩系列| 日韩精品久久久久| 国产精品成人va在线观看| 欧美日韩国产中文精品字幕自在自线| 青青草原成人在线视频| 日韩高清欧美高清| 欧美激情一级精品国产| 日韩激情视频在线| 中文字幕不卡av| 国产成人jvid在线播放| 亚洲自拍偷拍视频| 怡红院精品视频| 91在线看www| 中文字幕av一区二区| 久久久999国产| 久久久精品视频成人| 欧美肥臀大乳一区二区免费视频| 不用播放器成人网| 中文字幕精品一区久久久久| 欧美华人在线视频| 日韩在线视频中文字幕| 久久大大胆人体| 久久在精品线影院精品国产| 国产成人一区二区| 欧美午夜视频在线观看| 日韩av免费在线观看| 国产欧美一区二区三区视频| 亚洲字幕在线观看| 国产性猛交xxxx免费看久久| 欧美一级淫片丝袜脚交| 欧美色欧美亚洲高清在线视频| 国产精品成人免费视频| 国产欧美日韩免费看aⅴ视频| 久久的精品视频| 欧亚精品中文字幕| 精品国产福利视频| 成人日韩av在线| 91国产精品电影| 国内精品免费午夜毛片| 色香阁99久久精品久久久| 日韩免费观看网站| 亚洲毛片在线观看.| 久久国产精品影视| 尤物99国产成人精品视频| 亚洲自拍偷拍一区| 欧美日韩国产限制| 亚洲毛茸茸少妇高潮呻吟| 欧美性受xxxx黑人猛交| 欧美网站在线观看| 91精品在线观看视频| 国产精品久久97| 精品无人区太爽高潮在线播放| 欧美乱妇40p| 国产精品网站视频| 国产精品va在线播放| 欧美激情精品在线| 成人av在线天堂| 亚洲成色777777女色窝| 日本高清视频精品| 久久久久女教师免费一区| 日韩电影中文字幕一区| 国内久久久精品| 日韩免费不卡av| 亚洲国产精品va在线观看黑人| 欧美成人免费全部观看天天性色| 蜜月aⅴ免费一区二区三区| 欧美成人一区在线| 欧美激情在线观看| 国产亚洲精品久久久久动| 国产综合香蕉五月婷在线| 国产99久久久欧美黑人| 91国偷自产一区二区三区的观看方式| 亚洲精品av在线| 91香蕉亚洲精品| 成人天堂噜噜噜| 日韩视频免费大全中文字幕| 中文字幕日韩免费视频| 国产91网红主播在线观看| 国产精品激情av在线播放| 欧洲成人午夜免费大片| 97碰碰碰免费色视频| 欧美极品在线视频| 欧美久久精品午夜青青大伊人| 久久久久久久久久婷婷| 久久天天躁日日躁| 精品成人av一区| 91日韩在线视频| 欧美午夜片在线免费观看| 亚洲一级黄色片| 日韩视频欧美视频| 少妇精69xxtheporn| 久久偷看各类女兵18女厕嘘嘘| 亚洲男人天堂古典| 久久青草精品视频免费观看| 国产精品视频免费在线观看| 亚洲日韩欧美视频| 久久久久亚洲精品国产| 国产精品一区二区三区毛片淫片| 成人免费在线视频网址| 日韩欧美在线视频日韩欧美在线视频| 国产欧美一区二区三区久久| 久久久人成影片一区二区三区观看| 国产成人拍精品视频午夜网站| 黑人精品xxx一区一二区| 日韩欧美成人精品| 欧美贵妇videos办公室| 91中文精品字幕在线视频| 欧美大人香蕉在线| 日韩高清a**址| 91av在线不卡| 亚洲欧美日韩精品久久亚洲区| 成人字幕网zmw| 国产三级精品网站|