RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
jquery校验,jquery校验验证码的实现

jquery如何实现ajax验证验证码是否正确,加提示

把填写的验证码字符串通过ajax发送到后台,后台接收到字符串后,再从session里面找到实际的验证码字符串,两个对比,把对比的结果再给ajax,ajax再在页面做相应的提示就行了。

成都创新互联是专业的建华网站建设公司,建华接单;提供成都网站设计、网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行建华网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

如果想问ajax如何实现,建议用jquery吧,它已经封装好了ajax和后台交互的代码,只需要简单的写一个servlet,通过servlet来获取ajax提交的数据,校验工作在servlet里面处理,这样就能完成校验工作。

给一个提示:

request.getSession().getAttribute("session中变量的名称");

如何用jquery校验视频格式

如果有文件名的话,我能想到的就是用个正则判断文件扩展名是不是符合

function isVDOType(s) {

var patrn = /\w+(.flv|.rvmb|.mp4|.avi|.wmv)$/;

if (!patrn.exec(s)) return false;

return true

}

文件类型修改到需要的集合

jquery怎么校验字母和数字

正则表达式

整数或者小数:^[0-9]+\.{0,1}[0-9]{0,2}$

只能输入数字:"^[0-9]*$"。

只能输入n位的数字:"^\d{n}$"。

只能输入至少n位的数字:"^\d{n,}$"。

只能输入m~n位的数字:。"^\d{m,n}$"

只能输入零和非零开头的数字:"^(0|[1-9][0-9]*)$"。

只能输入有两位小数的正实数:"^[0-9]+(.[0-9]{2})?$"。

只能输入有1~3位小数的正实数:"^[0-9]+(.[0-9]{1,3})?$"。

只能输入非零的正整数:"^\+?[1-9][0-9]*$"。

只能输入非零的负整数:"^\-[1-9][]0-9"*$。

只能输入长度为3的字符:"^.{3}$"。

只能输入由26个英文字母组成的字符串:"^[A-Za-z]+$"。

只能输入由26个大写英文字母组成的字符串:"^[A-Z]+$"。

只能输入由26个小写英文字母组成的字符串:"^[a-z]+$"。

只能输入由数字和26个英文字母组成的字符串:"^[A-Za-z0-9]+$"。

只能输入由数字、26个英文字母或者下划线组成的字符串:"^\w+$"。

验证用户密码:"^[a-zA-Z]\w{5,17}$"正确格式为:以字母开头,长度在6~18之间,只能包含字符、数字和下划线。

验证是否含有^%',;=?$\"等字符:"[^%',;=?$\x22]+"。

只能输入汉字:"^[\u4e00-\u9fa5]{0,}$"

验证Email地址:"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"。

验证InternetURL:"^http://([\w-]+\.)+[\w-]+(/[\w-./?%=]*)?$"。

验证电话号码:"^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$"正确格式为:"XXX-XXXXXXX"、"XXXX-XXXXXXXX"、"XXX-XXXXXXX"、"XXX-XXXXXXXX"、"XXXXXXX"和"XXXXXXXX"。

验证身份证号(15位或18位数字):"^\d{15}|\d{18}$"。

验证一年的12个月:"^(0?[1-9]|1[0-2])$"正确格式为:"01"~"09"和"1"~"12"。

验证一个月的31天:"^((0?[1-9])|((1|2)[0-9])|30|31)$"正确格式为;"01"~"09"和"1"~"31"。 匹配中文字符的正则表达式: [\u4e00-\u9fa5]

匹配双字节字符(包括汉字在内):[^\x00-\xff]

应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)

String.prototype.len=function(){return this.replace(/[^\x00-\xff]/g,"aa").length;}

匹配空行的正则表达式:\n[\s| ]*\r

匹配html标签的正则表达式:(.*)(.*)\/(.*)|(.*)\/

匹配首尾空格的正则表达式:(^\s*)|(\s*$)

应用:javascript中没有像vbscript那样的trim函数,我们就可以利用这个表达式来实现,如下:

String.prototype.trim = function()

{

return this.replace(/(^\s*)|(\s*$)/g, "");

}

利用正则表达式分解和转换IP地址:

下面是利用正则表达式匹配IP地址,并将IP地址转换成对应数值的Javascript程序:

function IP2V(ip)

{

re=/(\d+)\.(\d+)\.(\d+)\.(\d+)/g //匹配IP地址的正则表达式

if(re.test(ip))

{

return RegExp.$1*Math.pow(255,3))+RegExp.$2*Math.pow(255,2))+RegExp.$3*255+RegExp.$4*1

}

else

{

throw new Error("Not a valid IP address!")

}

}

不过上面的程序如果不用正则表达式,而直接用split函数来分解可能更简单,程序如下:

var ip="10.100.20.168"

ip=ip.split(".")

alert("IP值是:"+(ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1))

匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

匹配网址URL的正则表达式:http://([\w-]+\.)+[\w-]+(/[\w- ./?%=]*)?

利用正则表达式限制网页表单里的文本框输入内容:

用正则表达式限制只能输入中文:onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))"

用正则表达式限制只能输入全角字符: onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))"

用正则表达式限制只能输入数字:onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"

用正则表达式限制只能输入数字和英文:onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"

匹配中文字符的正则表达式: [\u4e00-\u9fa5]

评注:匹配中文还真是个头疼的事,有了这个表达式就好办了

匹配双字节字符(包括汉字在内):[^\x00-\xff]

评注:可以用来计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)

匹配空白行的正则表达式:\n\s*\r

评注:可以用来删除空白行

匹配HTML标记的正则表达式:(\S*?)[^]*.*?|.*? /

评注:网上流传的版本太糟糕,上面这个也仅仅能匹配部分,对于复杂的嵌套标记依旧无能为力

匹配首尾空白字符的正则表达式:^\s*|\s*$

评注:可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式

匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

评注:表单验证时很实用

匹配网址URL的正则表达式:[a-zA-z]+://[^\s]*

评注:网上流传的版本功能很有限,上面这个基本可以满足需求

匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$

评注:表单验证时很实用

匹配国内电话号码:\d{3}-\d{8}|\d{4}-\d{7}

评注:匹配形式如 0511-4405222 或 021-87888822

匹配腾讯QQ号:[1-9][0-9]{4,}

评注:腾讯QQ号从10000开始

匹配中国邮政编码:[1-9]\d{5}(?!\d)

评注:中国邮政编码为6位数字

匹配身份证:\d{15}|\d{18}

评注:中国的身份证为15位或18位

匹配ip地址:\d+\.\d+\.\d+\.\d+

评注:提取ip地址时有用

匹配特定数字:

^[1-9]\d*$    //匹配正整数

^-[1-9]\d*$   //匹配负整数

^-?[1-9]\d*$ //匹配整数

^[1-9]\d*|0$  //匹配非负整数(正整数 + 0)

^-[1-9]\d*|0$ //匹配非正整数(负整数 + 0)

^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ //匹配正浮点数

^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$  //匹配负浮点数

^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$  //匹配浮点数

^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$ //匹配非负浮点数(正浮点数 + 0)

^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$//匹配非正浮点数(负浮点数 + 0)

评注:处理大量数据时有用,具体应用时注意修正

匹配特定字符串:

^[A-Za-z]+$//匹配由26个英文字母组成的字符串

^[A-Z]+$//匹配由26个英文字母的大写组成的字符串

^[a-z]+$//匹配由26个英文字母的小写组成的字符串

^[A-Za-z0-9]+$//匹配由数字和26个英文字母组成的字符串

^\w+$//匹配由数字、26个英文字母或者下划线组成的字符串

评注:最基本也是最常用的一些表达式

整理出来的一些常用的正则表达式所属分类: JScript  (三)

Email : /^\w+([-+.]\w+)*@\w+([-.]\\w+)*\.\w+([-.]\w+)*$/

isEmail1 : /^\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.\w+$/;

isEmail2 : /^.*@[^_]*$/;

Phone : /^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/

Mobile : /^((\(\d{3}\))|(\d{3}\-))?13\d{9}$/

Url : /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-_~`@[\]\':+!]*([^\"\"])*$/

IdCard : /^\d{15}(\d{2}[A-Za-z0-9])?$/

Currency : /^\d+(\.\d+)?$/

Number : /^\d+$/

Code : /^[1-9]\d{5}$/

QQ : /^[1-9]\d{4,8}$/

Integer : /^[-\+]?\d+$/

Double : /^[-\+]?\d+(\.\d+)?$/

English : /^[A-Za-z]+$/

Chinese : /^[\u0391-\uFFE5]+$/

UnSafe : /^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^\*\.\(\)\[\]\{\}\?\\\/\'\"]*)|.{0,5})$|\s/

PassWord :^[\\w]{6,12}$

ZipCode : ^[\\d]{6}

/^(\+\d+ )?(\(\d+\) )?[\d ]+$/; //这个是国际通用的电话号码判断

/^(1[0-2]\d|\d{1,2})$/; //这个是年龄的判断

/^\d+\.\d{2}$/;  //这个是判断输入的是否为货币值

!-- IP地址有效性验证函数--

script language=javascript runat=server

ip_ip = '(25[0-5]|2[0-4]\\d|1\\d\\d|\\d\\d|\\d)';

ip_ipdot = ip + '\\.';

isIPaddress = new RegExp('^'+ip_ipdot+ip_ipdot+ipdot+ip_ip+'$');

/script

应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)

String.prototype.len=function(){return this.replace([^\x00-\xff]/g,"aa").length;}

应用:javascript中没有像vbscript那样的trim函数,我们就可以利用这个表达式来实现,如下:

String.prototype.trim = function()

{

return this.replace(/(^\s*)|(\s*$)/g, "");

}

匹配空行的正则表达式:\n[\s| ]*\r

匹配HTML标记的正则表达式:/(.*).*\/\1|(.*) \//

匹配首尾空格的正则表达式:(^\s*)|(\s*$)

匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

匹配网址URL的正则表达式:http://([\w-]+\.)+[\w-]+(/[\w- ./?%=]*)?

^\d+$//匹配非负整数(正整数 + 0)

^[0-9]*[1-9][0-9]*$//匹配正整数

^((-\d+)|(0+))$//匹配非正整数(负整数 + 0)

^-[0-9]*[1-9][0-9]*$//匹配负整数

^-?\d+$//匹配整数

^\d+(\.\d+)?$//匹配非负浮点数(正浮点数 + 0)

^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$ //匹配正浮点数

^((-\d+(\.\d+)?)|(0+(\.0+)?))$//匹配非正浮点数(负浮点数 + 0)

^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$ //匹配负浮点数

^(-?\d+)(\.\d+)?$

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校验,jquery校验验证码的实现
链接URL:http://sczitong.cn/article/dscesij.html