您好,欢迎访问三七文档
当前位置:首页 > 医学/心理学 > 基础医学 > JavaScript表单验证大全
JavaScript表单验证大全(一)目录:1:js字符串长度限制、判断字符长度、js限制输入、限制不能输入、textarea长度限制2.:js判断汉字、判断是否汉字、只能输入汉字3:js判断是否输入英文、只能输入英文4:js只能输入数字,判断数字、验证数字、检测数字、判断是否为数字、只能输入数字5:只能输入英文字符和数字6:jsemail验证、js判断email、信箱/邮箱格式验证7:js字符过滤,屏蔽关键字8:js密码验证、判断密码2.1:js不为空、为空或不是对象、判断为空、判断不为空2.2:比较两个表单项的值是否相同2.3:表单只能为数字和_,2.4:表单项输入数值/长度限定2.5:中文/英文/数字/邮件地址合法性判断2.6:限定表单项不能输入的字符2.7表单的自符控制2.8:form文本域的通用校验函数1.长度限制scriptfunctiontest(){if(document.a.b.value.length50){alert(不能超过50个字符!);document.a.b.focus();returnfalse;}}/scriptformtest()textareacols=40wrap=VIRTUALrows=6/textareainputtype=submitvalue=check/form2.只能是汉字input/oblog/value.replace(/[^\u4E00-\u9FA5]/g,'')3.只能是英文scriptlanguage=javascriptfunctiononlyEng(){if(!(event.keyCode=65&&event.keyCode=90))event.returnvalue=false;}/scriptinput4.只能是数字scriptlanguage=javascriptfunctiononlyNum(){if(!((event.keyCode=48&&event.keyCode=57)||(event.keyCode=96&&event.keyCode=105)))//考虑小键盘上的数字键event.returnvalue=false;}/scriptinput5.只能是英文字符和数字input/oblog/value.replace(/[\W]/g,'')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=text7.屏蔽关键字(这里屏蔽***和****)scriptlanguage=javascript1.2functiontest(){if((a.b.value.indexOf(***)==0)||(a.b.value.indexOf(****)==0)){alert(:));a.b.focus();returnfalse;}}/scriptformtest()inputtype=textinputtype=submitvalue=check/form8.两次输入密码是否相同FORMMETHOD=POSTACTION=inputtype=passwordinputtype=passwordinputtype=buttonvalue=test/FORMscriptfunctioncheck(){with(document.all){if(input1.value!=input2.value){alert(false)input1.value=;input2.value=;}elsedocument.forms[0].submit();}}/script够了吧:)屏蔽右键很酷oncontextmenu=returnfalsefalsefalse加在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:是funct
本文标题:JavaScript表单验证大全
链接地址:https://www.777doc.com/doc-4810118 .html