양식 을 제출 하기 전에 검증 하 는 몇 가지 방식 으로 정리 합 니 다.

폼 을 제출 하기 전에 검증 하 는 몇 가지 방법 입 니 다.Django 에 서 는 배경 압력 을 줄 이기 위해 자바 스 크 립 트 를 이용 하여 폼 을 제출 하기 전에 폼 데 이 터 를 검증 할 수 있 습 니 다.다음은 효과 적 인 몇 가지 방식 을 제공 합 니 다.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 설정 및 관련 설정

좋은 웹페이지 즐겨찾기