在表單提交前進行驗證的幾種方式
在Django中
formpage
<!DOCTYPE html PUBLIC "
<html xmlns="
<head>
<meta http
<title>Example
<script type="text/javascript" src="/Resource/jquery
<script type="text/javascript">
function jump()
{
//清空表單所有數據
document
document
$("#firstnameLabel")
$("#lastnameLabel")
}
$(document)
$("#form
var txt_firstname = $
var txt_lastname = $
$("#firstnameLabel")
$("#lastnameLabel")
var isSuccess =
if(txt_firstname
{
$("#firstnameLabel")
$("#firstnameLabel")
isSuccess =
}
if(txt_lastname
{
$("#lastnameLabel")
$("#lastnameLabel")
isSuccess =
}
if(isSuccess ==
{
return false;
}
})
})
</script>
</head>
<body>
提交表單前進行驗證(方法一)
<hr width="
<form id="form
<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="
<button type="submit">提交</button>
<button type="button" onclick="jump();">取消</button>
</form>
</body>
</html>
formpage
<!DOCTYPE html PUBLIC "
<html xmlns="
<head>
<meta http
<title>Example
<script type="text/javascript" src="/Resource/jquery
<script type="text/javascript">
function jump()
{
//清空表單所有數據
document
document
$("#firstnameLabel")
$("#lastnameLabel")
}
function check(){
var txt_firstname = $
var txt_lastname = $
$("#firstnameLabel")
$("#lastnameLabel")
var isSuccess =
if(txt_firstname
{
$("#firstnameLabel")
$("#firstnameLabel")
isSuccess =
}
if(txt_lastname
{
$("#lastnameLabel")
$("#lastnameLabel")
isSuccess =
}
if(isSuccess ==
{
return false;
}
return true;
}
</script>
</head>
<body>
提交表單前進行驗證(方法二)
<hr width="
<form id="form
<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="
<button type="submit">提交</button>
<button type="button" onclick="jump();">取消</button>
</form>
</body>
</html>
formpage
<!DOCTYPE html PUBLIC "
<html xmlns="
<head>
<meta http
<title>Example
<script type="text/javascript" src="/Resource/jquery
<script type="text/javascript">
function jump()
{
//清空表單所有數據
document
document
$("#firstnameLabel")
$("#lastnameLabel")
}
function checktosubmit(){
var txt_firstname = $
var txt_lastname = $
$("#firstnameLabel")
$("#lastnameLabel")
var isSuccess =
if(txt_firstname
{
$("#firstnameLabel")
$("#firstnameLabel")
isSuccess =
}
if(txt_lastname
{
$("#lastnameLabel")
$("#lastnameLabel")
isSuccess =
}
if(isSuccess ==
{
form
}
}
</script>
</head>
<body>
提交表單前進行驗證(方法三)
<hr width="
<form id="form
<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="
<button type="button" onclick="checktosubmit()">提交</button>
<button type="button" onclick="jump();">取消</button>
</form>
</body>
</html>
以下是視圖函數
views
#coding: utf
from django
from django
def DealWithForm
if request
FirstName=request
LastName=request
if FirstName and LastName:
response=HttpResponse()
response
return response
else:
response=HttpResponse()
response
window
return response
else:
return render_to_response(
def DealWithForm
if request
FirstName=request
LastName=request
if FirstName and LastName:
html="<html><body>"+FirstName+" "+LastName+"! 你提交了表單!"+"</body></html>"
return HttpResponse(html)
else:
response=HttpResponse()
response
window
return response
else:
return render_to_response(
def DealWithForm
if request
FirstName=request
LastName=request
if FirstName and LastName:
response=HttpResponse()
response
return response
else:
response=HttpResponse()
response
window
return response
else:
return render_to_response(
urls
from django
import views
from django
urlpatterns = patterns(
url(r
url(r
url(r
url(r
)
settings
# Django settings for CheckFormBeforeSubmit project
import os
HERE = os
DEBUG = True
TEMPLATE_DEBUG = DEBUG
STATIC_RESOURCE=os
MIDDLEWARE_CLASSES = (
)
ROOT_URLCONF =
TEMPLATE_DIRS = (
os
# Put strings here
# Always use forward slashes
# Don
)
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20397.html