JavaScript验证表单

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

JavaScript验证表单1.长度限制scriptfunctiontest(){if(document.a.b.value.length50){alert(不能超过50个字符!);document.a.b.focus();returnfalse;}}/scriptformname=aonsubmit=returntest()textareaname=bcols=40wrap=VIRTUALrows=6/textareainputtype=submitname=Submitvalue=check/form2.只能是汉字inputonkeyup=value=/oblog/value.replace(/[^\u4E00-\u9FA5]/g,'')3.只能是英文scriptlanguage=javascriptfunctiononlyEng(){if(!(event.keyCode=65&&event.keyCode=90))event.returnvalue=false;}/scriptinputonkeydown=onlyEng();4.只能是数字scriptlanguage=javascriptfunctiononlyNum(){if(!((event.keyCode=48&&event.keyCode=57)||(event.keyCode=96&&event.keyCode=105)))//考虑小键盘上的数字键event.returnvalue=false;}/scriptinputonkeydown=onlyNum();5.只能是英文字符和数字inputonkeyup=value=/oblog/value.replace(/[\W]/g,'')onbeforepaste=clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))6.验证油箱格式SCRIPTLANGUAGE=javascriptRUNAT=ServerfunctionisEmail(strEmail){if(strEmail.search(/^\w((-\w)|(\.\w))*\@[A-Za-z0-9]((\.|-)[A-Za-z0-9])*\.[A-Za-z0-9]$/)!=-1)returntrue;elsealert(oh);}/SCRIPTinputtype=textonblur=isEmail(this.value)7.屏蔽要害字(这里屏蔽***和****)scriptlanguage=javascript1.2functiontest(){if((a.b.value.indexOf(***)==0)||(a.b.value.indexOf(****)==0)){alert(:));a.b.focus();returnfalse;}}/scriptformname=aonsubmit=returntest()inputtype=textname=binputtype=submitname=Submitvalue=check/form8.两次输入密码是否相同FORMMETHOD=POSTACTION=inputtype=passwordid=input1inputtype=passwordid=input2inputtype=buttonvalue=testonclick=check()/FORMscriptfunctioncheck(){with(document.all){if(input1.value!=input2.value){alert(false)input1.value=;input2.value=;}elsedocument.forms[0].submit();}}/script够了吧:)屏蔽右键很酷oncontextmenu=returnfalseondragstart=returnfalseonselectstart=returnfalse加在body中二2.1表单项不能为空scriptlanguage=javascript!--functionCheckForm(){if(document.form.name.value.length==0){alert(请输入您姓名!);document.form.name.focus();returnfalse;}returntrue;}--/script2.2比较两个表单项的值是否相同scriptlanguage=javascript!--functionCheckForm()if(document.form.PWD.value!=document.form.PWD_Again.value){alert(您两次输入的密码不一样!请重新输入.);document.ADDUser.PWD.focus();returnfalse;}returntrue;}--/script2.3表单项只能为数字和_,用于电话/银行帐号验证上,可扩展到域名注册等scriptlanguage=javascript!--functionisNumber(String){varLetters=1234567890-;//可以自己增加可输入值vari;varc;if(String.charAt(0)=='-')returnfalse;if(String.charAt(String.length-1)=='-')returnfalse;for(i=0;iString.length;i){c=String.charAt(i);if(Letters.indexOf(c)0)returnfalse;}returntrue;}functionCheckForm(){if(!isNumber(document.form.TEL.value)){alert(您的电话号码不合法!);document.form.TEL.focus();returnfalse;}returntrue;}--/script2.4表单项输入数值/长度限定scriptlanguage=javascript!--functionCheckForm(){if(document.form.count.value100||document.form.count.value1){alert(输入数值不能小于零大于100!);document.form.count.focus();returnfalse;}if(document.form.MESSAGE.value.length10){alert(输入文字小于10!);document.form.MESSAGE.focus();returnfalse;}returntrue;}//--/script2.5中文/英文/数字/邮件地址合法性判定SCRIPTLANGUAGE=javascript!--functionisEnglish(name)//英文值检测{if(name.length==0)returnfalse;for(i=0;iname.length;i){if(name.charCodeAt(i)128)returnfalse;}returntrue;}functionisChinese(name)//中文值检测{if(name.length==0)returnfalse;for(i=0;iname.length;i){if(name.charCodeAt(i)128)returntrue;}returnfalse;}functionisMail(name)//E-mail值检测{if(!isEnglish(name))returnfalse;i=name.indexOf(at);j=namedotlastIndexOf(at);if(i==-1)returnfalse;if(i!=j)returnfalse;if(i==namedotlength)returnfalse;returntrue;}functionisNumber(name)//数值检测{if(name.length==0)returnfalse;for(i=0;iname.length;i){if(name.charAt(i)0||name.charAt(i)9)returnfalse;}returntrue;}functionCheckForm(){if(!isMail(form.Email.value)){alert(您的电子邮件不合法!);form.Email.focus();returnfalse;}if(!isEnglish(form.name.value)){alert(英文名不合法!);form.name.focus();returnfalse;}if(!isChinese(form.cnname.value)){alert(中文名不合法!);form.cnname.focus();returnfalse;}if(!isNumber(form.PublicZipCode.value)){alert(邮政编码不合法!);form.PublicZipCode.focus();returnfalse;}returntrue;}//--/SCRIPT2.6限定表单项不能输入的字符scriptlanguage=javascript!--functioncontain(str,charset)//字符串包含测试函数{vari;for(i=0;icharset.length;i)if(str.indexOf(charset.charAt(i))=0)returntrue;returnfalse;}functionCheckForm(){if((contain(document.form.NAME.value,%\(\)))||(contain(document.form.MESSAGE.value,%\(\)))){alert(输入了非法字符);document.form.NAME.focus();returnfalse;}returntrue;}//--/script1.检查一段字符串是否全由数字组成---------------------------------------scriptlanguage=Javascript!--functioncheckNum(str){returnstr.match(/\D/)==null}alert(checkNum(1232142141))alert(checkNum(123214214a1))//--/script2.怎么判定是否是字符---------------------------------------if(/[^\x00-\xff]/g.test(s))alert(含有汉字);elsealert(全是字符);3.怎么判定是否含有汉字---------------------------------------if(escape(str).indexOf(%u)!=-1)alert(含有汉字);elsealert(全是字符);4.邮箱格式验证---------------------------------------//函数名:chkemail//功能介绍:检查是否为EmailAddress//参数说明:要检查的字符串//返回值:0:不是1:是functionchkemail(a){vari=a.length;vartemp=a.indexOf('@');vartempd=a.indexOf('.');if(temp

1 / 30
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功