Home  >  Article  >  Web Front-end  >  JavaScript form validation encyclopedia

JavaScript form validation encyclopedia

高洛峰
高洛峰Original
2016-11-26 10:11:451124browse

<script><br>function test()<br>{<br>if(document.a.b.value.length>50)<br>{<br>alert("Cannot exceed 50 characters!");<br>document.a.b.focus();<br>return false ;<br>}<br>}<br></script>




2. Can only be Chinese characters

3. Only Can it be in English

<script><br>function onlyEng()<br>{<br>if(!(event.keyCode>=65&&event.keyCode<=90))<br>event.returnvalue=false;<br>}<br></script>

;=96&&event.keyCode<=105)))

//Consider the numeric keys on the small keyboard

event.returnvalue=false;

}





5. Only in English Characters and numbers

6. Verify email format




7. Block keywords ( *** and **** are blocked here)

<script><br>function test() {<br>if((a.b.value.indexOf ("***") == 0)||(a.b.value.indexOf ( "****") == 0)){<br>alert(":)");</p>a.b.focus();<p>return false;}</p>}<p></script>




8. Are the passwords entered twice the same?



< input>

<script></p>function check()<p>{<br>with(document.all){<br>if(input1.value!=input2.value )<br>{<br>alert("false")<br>input1.value = "";<br>input2.value = "";<br>}<br>else document.forms[0].submit();<br>}<br>}<br></script&gt ;<br><br><br>That’s enough:)<br>It’s cool to shield the right click<br><br>oncontextmenu="return false"<br></p>Add it to the body<p><br><br>二</p> <p>2.1 The form item cannot be empty</p> <p><script></p><! --<p>function CheckForm()<br/>{</p>if (document.form.name.value.length == 0) {<p>alert("Please enter your name!");</p>document.form.name.focus();<p> return false;<br/>}<br/>return true;<br/>}<br/>--><br></script>

2.2 Compare whether the values ​​of two form items are the same

<script><br><!--<br/>function CheckForm( )<br/>if (document.form.PWD.value != document.form.PWD_Again.value) {</p>alert("The passwords you entered twice are different! Please re-enter.");<p>document.ADDUser.PWD.focus();</p>return false;<p>}<br/>return true;<br/>}<br/>--><br></script>

2.3 Form items can only be numbers and "_", used for phone/bank account verification, can be extended to domain name registration, etc.

<script><br><!--<br/>function isNumber(String)<br/>{</p>var Letters = "1234567890-"; / /You can add input values ​​by yourself<p>var i;</p>var c;<p>if(String.charAt(0)=='-')<br/>return false;<br/>if(String.charAt(String.length - 1) == ' -' )<br/>return false;<br/>for( i = 0; i < String.length; i ++ )<br/>{<br/>c = String.charAt( i );<br/>if (Letters.indexOf( c ) < 0) <br/>return false;<br/>}<br/>return true;<br/>}<br/>function CheckForm()<br/>{<br/>if(! isNumber(document.form.TEL.value)) {<br/>alert("Your phone number is illegal!");<br/> document.form.TEL.focus();<br/>return false;<br/>}<br/>return true;<br/>}<br/>--><br></script>


2.4 Form item input value/length limit

<script> <br><!--<br/>function CheckForm()</p>{<p>if (document.form.count.value > 100 || document.form.count.value < 1)<br/>{</p>alert("The input value cannot be less than zero Greater than 100!");<p>document.form.count.focus();<br/>return false;<br/>}<br/>if (document.form.MESSAGE.value.length<10)<br/>{<br/>alert("The input text is less than 10!" );<br/>document.form.MESSAGE.focus();<br/>return false;<br/>}<br/>return true;<br/>}<br/>//--><br></script>

2.5 Chinese/English/numeric/email address validity judgment

<SCRIPT><br><!--<br/>function isEnglish(name) //English value detection<br/>{<br/>if(name.length == 0)<br/>return false;<br/>for(i = 0; i < name.length; i++) {<br/>if(name.charCodeAt(i) > 128)<br>return false;<br>}<br>return true;<br>}<br>function isChinese(name) //Chinese value detection<br>{<br>if(name.length == 0)<br>return false;<br>for(i = 0; i < name.length; i++) {<br/>if(name.charCodeAt(i) > 128 )<br>return true;<br>}<br>return false;<br>}<br>function isMail(name) //E-mail value detection<br>{<br>if(! isEnglish(name))<br>return false;<br>i = name.indexOf(" at ");<br>j = name dot lastIndexOf(" at ");<br>if(i == -1)<br>return false;<br>if(i != j)<br>return false;<br>if(i == name dot length) <br>return false;<br>return true;<br>}<br>function isNumber(name) //Number detection<br>{<br>if(name.length == 0)<br>return false;<br>for(i = 0; i < name.length; i++) {<br/>if(name.charAt(i) < "0" || name.charAt(i) > "9")<br>return false;<br>}<br>return true;<br>}<br>function CheckForm()<br>{ <br>if(! isMail(form.Email.value)) {<br>alert("Your email is invalid!");<br>form.Email.focus();<br>return false;<br>}<br>if(! isEnglish(form .name.value)) {<br>alert("The English name is illegal!");<br>form.name.focus();<br>return false;<br>}<br>if(! isChinese(form.cnname.value)) {<br>alert ("The Chinese name is illegal! ");<br>form.cnname.focus();<br>return false;<br>}<br>if(! isNumber(form.PublicZipCode.value)) {<br>alert("The postal code is illegal!");<br>form.PublicZipCode.focus ();<br>return false;<br>}<br>return true;<br>}<br>//--><br></SCRIPT>

2.6 Limit the characters that cannot be entered in the form item

<script><br><!-- <br/>function contain(str,charset)//String contains test function<br/>{<br/>var i;<br/>for(i=0;i<charset.length;i++)<br/>if(str.indexOf(charset.charAt(i)) >=0)<br>return true;<br>return false;<br>}<br>function CheckForm()<br>{<br>if ((contain(document.form.NAME.value, "%()><")) || ( contain(document.form.MESSAGE.value, "%</p><p>()><")))<br/>{<br/>alert("Illegal characters entered");<br/>document.form.NAME.focus();<br/>return false;<br/>}<br/>return true;<br/>}<br/>//--><br></script>

1. Check whether a string consists entirely of numbers
------------- ---------------------------


& lt; script & gt; & lt;!-
Function Checknum (STR) {Return Str.match ( +​
---------------------------------------
if (/[^x00-xff]/ g.test(s)) alert("Contains Chinese characters");      
else alert("All characters"); --------------------------
if (escape(str).indexOf("%u")!=-1) alert("Contains Chinese characters ");
else alert("All characters");

4. Email format verification
----------------------------- -----------
//Function name: chkemail
//Function introduction: Check whether it is an Email Address
//Parameter description: The string to be checked
//Return value: 0: not 1 :Yes
function chkemail(a)

{ var i=a.length;

var temp = a.indexOf('@');
var tempd = a.indexOf('.');
if (temp > 1 ) {
if ((i-temp) > 3){
if ((i-tempd)>0){
return 1;
}

}
}
return 0;
}

5. Number Format verification
---------------------------------------------

//Function name: fucCheckNUM

//Function introduction: Check whether it is a number
//Parameter description: The number to be checked
//Return value: 1 means it is a number, 0 means it is not a number
function fucCheckNUM(NUM)

{

var i,j,strTemp;
strTemp="0123456789";
if (NUM.length== 0)
return 0
for (i=0;i{
j=strTemp.indexOf(NUM.charAt(i) );
if (j==-1)
{
//Indicates that the character is not a number
return 0;
}
}
//Indicates that it is a number
return 1;
}

6. Phone number format verification
---------------------------------------------
// Function name: fucCheckTEL
//Function introduction: Check whether it is a phone number
//Parameter description: The string to be checked
//Return value: 1 is legal, 0 is illegal
function fucCheckTEL(TEL)
{
var i,j,strTemp;
strTemp="0123456789-()# ";
for (i=0;i{
j=strTemp.indexOf(TEL.charAt(i));
if (j==-1)
{
//Indicates that the characters are illegal
return 0;
}
}
//Indicates that it is legal www.2cto.com
return 1;
}

7. Determine whether the input is a Chinese function
---------------------------------------------
function ischinese(s) {
var ret=true;
for(var i=0;iret=ret && (s.charCodeAt(i)>=10000);
return ret;
}

8 . Comprehensive function to judge the legality of user input
------------------------------------------------ -
<script><br>//Start by limiting the number of digits to be entered<br>//m is user input, n is the number of digits to be limited<br>function issmall(m,n)<br>{<br>if ((m<n) && (m>0))<br>{<br>return(false);<br>}<br>else<br>{return(true);}<br>}</p> <p>9. Determine whether the passwords are entered consistently<br>------------ -------------------------- <br>function issame(str1,str2)<br>{<br>if (str1==str2)<br>{return(true );}<br>else<br>{return(false);}<br>}</p> <p>10. Determine whether the user name is a numeric letter underline <br>--------------------- ------------------ <br>function notchinese(str){<br>var reg=/[^A-Za-z0-9_]/g<br>if (reg.test(str )){<br>return (false);<br>}else{<br>return(true); }<br>}</p> <p>11. General verification function for form text field<br>-------------------------- -------------------------- <br>Function: Detect all input text that must be non-empty, such as name, account number, email address, etc. <br>This check is now only for the text field. If you want to target other field objects in the form, you can change the judgment conditions. </p> <p>How to use: Add title text to the text field to be detected. The text is the prompt information. You want to prompt the Chinese name of the field to the user. For example, if you want to detect </p> <p>username<br>html as follows <input title="Name">, of course, it is best to use a visual tool such as Dreamweaver to edit the domain. <br>If you want to detect numeric type data, then unify the domain ID to sz. <br>It is troublesome for JavaScript to determine the date type, so there is no program for date type verification. Experts can add. </p> <p>The program is relatively crude, I just provide an idea. Throw some bricks to attract good news! :)<br>Oh, by the way, function calling method: < form></p> <p>function dovalidate()<br>{<br>fm=document.forms[0] //Only detect one form, if there are multiple, you can change the judgment condition<br>for (i=0;i<fm.length;i++)<br>{<br>//Detection and judgment conditions, which can be modified according to different types<br>if(fm[i].tagName.toUpperCase()=="INPUT" &&fm[i]. type.toUpperCase()=="TEXT" && (fm[i].title!=""))</p> <p> if(fm[i].value="/blog/="")//<br> {<br> str_warn1= fm[i].title+"Can't be empty!";<br> alert(str_warn1);<br> fm[i].focus();<br> return false; }<br> if(fm[i].id.toUpperCase()== "SZ")//Digital verification<br>                                                                                                                                                                         fm[i] .focus();<br>                                                                    return false; <br><br><script><br><! --<br/>function CheckForm()<br/>{<br/>if (document.form.name.value.length == 0) {<br/>alert("Please enter your name!");<br/>document.form.name.focus();<br/> return false;<br/>}</p>return true;<p>}<br/>--></p></script>


2.2 Compare whether the values ​​of two form items are the same

<script><br><!--<br/>function CheckForm()<br/>if (document.form.PWD.value != document.form.PWD_Again.value) {<br/>alert("The passwords you entered twice are different! Please re-enter.");<br/>document.ADDUser.PWD.focus();<br/>return false;<br/>}<br/>return true;<br/>}<br/>--><br></script>

2.3 Form items can only be numbers and "_", used for phone/bank account verification, can be extended to domain name registration, etc.

<script><br><!--<br/>function isNumber(String)<br/>{<br/>var Letters = "1234567890-"; / /You can add input values ​​by yourself<br/>var i;<br/>var c;<br/>if(String.charAt(0)=='-')<br/>return false;<br/>if(String.charAt(String.length - 1) == ' -' )<br/>return false;<br/>for( i = 0; i < String.length; i ++ )<br/>{<br/>c = String.charAt( i );<br/>if (Letters.indexOf( c ) < 0) <br/>return false;<br/>}<br/>return true;<br/>}<br/>function CheckForm()<br/>{<br/>if(! isNumber(document.form.TEL.value)) {<br/>alert("Your phone number is illegal!");<br/> document.form.TEL.focus();<br/>return false;<br/>}<br/>return true;<br/>}<br/>--><br></script>


2.4 Form item input value/length limit

<script> <br><!--<br/>function CheckForm()<br/>{<br/>if (document.form.count.value > 100 || document.form.count.value < 1)<br/>{<br/>alert("The input value cannot be less than zero Greater than 100!");<br/>document.form.count.focus();<br/>return false;<br/>}<br/>if (document.form.MESSAGE.value.length<10)<br/>{<br/>alert("The input text is less than 10!" );<br/>document.form.MESSAGE.focus();<br/>return false;<br/>}<br/>return true;<br/>}<br/>//--><br></script>

2.5 Chinese/English/numeric/email address is legal Sex judgment

<SCRIPT><br><!--</p> <p>function isEnglish(name) //English value detection<br>{<br>if(name.length == 0)<br>return false;<br>for(i = 0; i < name.length; i++) {<br/>if(name.charCodeAt(i) > 128)<br>return false;<br>}<br>return true;<br>}</p> <p>function isChinese(name) //Chinese value detection<br>{<br>if (name.length == 0)<br>return false;<br>for(i = 0; i < name.length; i++) {<br/>if(name.charCodeAt(i) > 128)<br>return true;<br>}<br>return false;<br>}</p> <p>function isMail(name) //E-mail value detection<br>{<br>if(! isEnglish(name))<br>return false;<br>i = name.indexOf(" at ");<br>j = name dot lastIndexOf(" at ");<br>if(i == -1)<br>return false;<br>if(i != j)<br>return false;<br>if(i == name dot length)<br>return false;<br>return true; <br>}</p> <p>function isNumber(name) //Numerical detection<br>{<br>if(name.length == 0)<br>return false;<br>for(i = 0; i < name.length; i++) {<br/>if(name .charAt(i) < "0" || name.charAt(i) > "9")<br>return false;<br>}<br>return true;<br>}</p> <p>function CheckForm()<br>{<br>if(! isMail( form.Email.value)) {<br>alert("Your email is illegal! ");<br>form.Email.focus();<br>return false;<br>}<br>if(! isEnglish(form.name.value)) {<br>alert("The English name is illegal!");<br>form.name.focus ();<br>return false;<br>}<br>if(! isChinese(form.cnname.value)) {<br>alert("The Chinese name is illegal!");<br>form.cnname.focus();<br>return false;<br>} <br>if(! isNumber(form.PublicZipCode.value)) {<br>alert("The postal code is illegal!");<br>form.PublicZipCode.focus();<br>return false;<br>}<br>return true;<br>}<br>// --><br></SCRIPT>

2.6 Limit the characters that cannot be entered in the form item

<script><br><!--</p><p>function contain(str,charset)//The string contains the test function<br/>{ <br/>var i;<br/>for(i=0;i<charset.length;i++)<br/>if(str.indexOf(charset.charAt(i))>=0)<br>return true;<br>return false;<br>}</p> <p> function CheckForm()<br>{<br>if ((contain(document.form.NAME.value, "%()><")) || (contain(document.form.MESSAGE.value, "%</p><p>() ><")))<br>{<br>alert("Illegal characters entered");<br>document.form.NAME.focus();<br>return false;<br>}<br>return true;<br>}<br>//--&gt ;<br></script>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn