首頁 > web前端 > js教程 > 主體

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

WBOY
發布: 2016-05-16 17:27:33
原創
1055 人瀏覽過

在表單提交前進行驗證的幾種方式 .
在Django中,為了減輕後台壓力,可以利用JavaScript在表單提交前對表單資料進行驗證。下面提供了有效的幾種方式(每個.html檔案為一種方式)。
formpage1.html

複製程式碼 程式碼如下:


程式碼如下:


UBL -//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



Example1




提交表單前進行驗證(方法一)














first_name:
last_name:





複製代碼 代碼如下:




>
範例2標題>

formpage3.html
複製程式碼程式碼如下:




>
範例3標題>

以下是視圖函數、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','')
FirstName と LastName の場合:
応答=HttpResponse()
response.write("" FirstName " " LastName u"!你提交了表单!")
応答を返す
else:
response=HttpResponse()
response.write('')
応答を返す
else:
return render_to_response('formpage1.html')
def DealWithForm2 (リクエスト):
if request.method=="POST":
FirstName=request.POST.get('firstname','').encode("utf-8")
LastName=request .POST.get('lastname','').encode("utf-8")
FirstName と LastName の場合:
html="" FirstName " " LastName "!你提交了表单!" ""
return HttpResponse(html)
else:
response=HttpResponse()
response.write('')
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('' FirstName LastName u'!你
レスポンスを返す
else:
response=HttpResponse()
response.write('<スクリプト タイプ="text/javascript">alert("名または姓は空にはできません!");
window.location="/DealWithForm3"')
応答を返します
else:
return render_to_response('formpage3.html')

urls.py
复制代 代码如下:

django.conf.urls.defaults からパターンをインポート、インクルード、URL
ビューをインポート
django.conf から設定をインポート
urlpatterns = パターン('',
url(r'^Resource/(?P.*)$','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
复制代码代码如下:

# CheckFormBeforeSubmit プロジェクトの Django 設定。
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'),
# ここに文字列を入れます, "/home/html/django_templates" や "C:/www/django/templates" のように。
# Windows であっても、常にスラッシュを使用してください。
# 相対パスではなく、絶対パスを使用することを忘れないでください。 .
)
...
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!