免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

表單驗證jquery,表單驗證是指

jQuery為什么需要進行表單驗證

不是jQuery需要進行表單驗證, 是網頁在提交數據的時候,為了減輕服務器的活,把能做的都在前端做了。

成都創新互聯公司專注于石景山企業網站建設,響應式網站建設,成都做商城網站。石景山網站建設公司,為石景山等地區提供建站服務。全流程按需網站策劃,專業設計,全程項目跟蹤,成都創新互聯公司專業和態度為您提供的服務

比如,用戶輸入一個手機號碼,如果該手機號碼格式是錯的,比如格式是158125238pp

然后前端人員又沒有對數據進行驗證,然后又提交到服務器那里去,很顯然這個手機號是錯的,服務器存儲這個手機號一點用處也沒有、、、

所以需要進行表單驗證

jquery驗證表單是否為空

jquery判斷表單提交內容是否為空

按照代碼就能實現。

簡單代碼如下:

$(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(“對不起,請填寫內容!”)

$(“input[name='txtcontent']“).focus();

return false

}

if ($(“input[name='txtcontent']“).val().length 150){

alert(“對不起,內容超過150個字符限制!”)

$(“input[name='txtcontent']“).focus();

return false

}})

$(“#t”).keyup(function(){

$(“.inner”).text($(“input[name='txtcontent']“).val());

}).change(function(){

$(“.inner”).text($(“input[name='txtcontent']“).val());

});

});

jquery在表單提交前有幾種校驗方法

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

在Django中,為了減輕后臺壓力,可以利用JavaScript在表單提交前對表單數據進行驗證。下面提供了有效的幾種方式(每個.html文件為一種方式)。

formpage1.html

復制代碼 代碼如下:

!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()

{

//清空表單所有數據

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

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

復制代碼 代碼如下:

!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()

{

//清空表單所有數據

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

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

復制代碼 代碼如下:

!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()

{

//清空表單所有數據

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

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

以下是視圖函數、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("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

復制代碼 代碼如下:

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

復制代碼 代碼如下:

# 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,表單驗證是指
URL標題:http://m.newbst.com/article42/phheec.html

成都網站建設公司_創新互聯,為您提供響應式網站企業建站電子商務搜索引擎優化微信公眾號手機網站建設

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都網頁設計公司