Copy codeThe Code is as follows:
<Script language = "javascript">
<! --
Function form1_onsubmit ()
{
Var urlreg =/^ [A-Za-z0-9] + \. [A-Za-z0-9] + [\/= \? % \-&_~ '@ [\] \': +!] * ([^ <> \ "\"]) * $/
Var url = document. form1.url. value
If (! Urlreg. test (url ))
{
Alert ("website error! ")
Document. form1.url. focus ()
Return false
}
}
-->
</Script>
<Form name = "form1" method = "post" action = "" onsubmit = "return form1_onsubmit ()">
Http: // <input type = "text" name = "url" value = ">
<Input type = "submit" name = "submit" value = "verify">
</Form>
Javascript verification form Overview
1. length limit
Copy codeThe Code is as follows:
<Script>
Function test ()
{
If (document. a. B. value. length> 50)
{
Alert ("cannot exceed 50 characters! ");
Document. a. B. focus ();
Return false;
}
}
</Script>
<Form name = a onsubmit = "return test ()">
<Textarea name = "B" cols = "40" wrap = "VIRTUAL" rows = "6"> </textarea>
<Input type = "submit" name = "Submit" value = "check">
</Form>
2. Only Chinese characters are allowed
<Input onkeyup = "value ="/oblog/value. replace (/[^ \ u4E00-\ u9FA5]/g, '')">
3. "English only
Copy codeThe Code is as follows:
<Script language = javascript>
Function onlyEng ()
{
If (! (Event. keyCode> = 65 & event. keyCode <= 90 ))
Event. returnvalue = false;
}
</Script>
<Input onkeydown = "onlyEng ();">
4. Only numbers are allowed.
Copy codeThe Code is as follows:
<Script language = javascript>
Function onlyNum ()
{
If (! (Event. keyCode> = 48 & event. keyCode <= 57) | (event. keyCode> = 96 & event. keyCode <= 105 )))
// Consider the numeric keys on the keypad
Event. returnvalue = false;
}
</Script>
<Input onkeydown = "onlyNum ();">
5. Only English characters and numbers are allowed.
Copy codeThe Code is as follows:
<Input onkeyup = "value ="/oblog/value. replace (/[\ W]/g, "'') "onbeforepaste =" clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^ \ d]/g, '')">
6. Verify the fuel tank format
Copy codeThe Code is as follows:
<Script language = javascript RUNAT = Server>
Function isEmail (strEmail ){
If (strEmail. search (/^ \ w + (-\ w +) | (\. \ w +) * \ @ [A-Za-z0-9] + ((\. |-) [A-Za-z0-9] + )*\. [A-Za-z0-9] + $ /)! =-1)
Return true;
Else
Alert ("oh ");
}
</SCRIPT>
<Input type = text onblur = isEmail (this. value)>
7. Shielding keywords (here, shielding ***** and ****)
Copy codeThe Code is as follows:
<Script language = "javascript1.2">
Function test (){
If (. b. value. indexOf ("***") = 0) | (. b. value. indexOf ("***") = 0 )){
Alert (":)");
A. B. focus ();
Return false ;}
}
</Script>
<Form name = a onsubmit = "return test ()">
<Input type = text name = B>
<Input type = "submit" name = "Submit" value = "check">
</Form>
8. Whether the two passwords are the same
Copy codeThe Code is as follows:
<Form method = post action = "">
<Input type = "password" id = "input1">
<Input type = "password" id = "input2">
<Input type = "button" value = "test" onclick = "check ()">
</FORM>
<Script>
Function check ()
{
With (document. all ){
If (input1.value! = Input2.value)
{
Alert ("false ")
Input1.value = "";
Input2.value = "";
}
Else document. forms [0]. submit ();
}
}
</Script>
Enough :)
Blocking right-click is cool
Oncontextmenu = "return false" ondragstart = "return false" onselectstart = "return false"
Add to body
II
2.1 Form items cannot be blank
Copy codeThe Code is as follows:
<Script language = "javascript">
<! --
Function CheckForm ()
{
If (document. form. name. value. length = 0 ){
Alert ("enter your name! ");
Document. form. name. focus ();
Return false;
}
Return true;
}
-->
</Script>
2.2 compare whether the values of the two form items are the same
Copy codeThe Code is as follows:
<Script language = "javascript">
<! --
Function CheckForm ()
If (document. form. PWD. value! = Document. form. PWD_Again.value ){
Alert ("the password you entered twice is different! Enter again .");
Document. ADDUser. PWD. focus ();
Return false;
}
Return true;
}
-->
</Script>
2.3 form items can only be numbers and "_", used for phone/Bank Account Verification, and can be extended to domain name registration.
Copy codeThe Code is as follows:
<Script language = "javascript">
<! --
Function isNumber (String)
{
Var Letters = "1234567890-"; // you can add input values by yourself.
Var I;
Var c;
If (String. charAt (0) = '-')
Return false;
If (String. charAt (String. length-1) = '-')
Return false;
For (I = 0; I <String. length; I ++)
{
C = String. charAt (I );
If (Letters. indexOf (c) <0)
Return false;
}
Return true;
}
Function CheckForm ()
{
If (! IsNumber (document. form. TEL. value )){
Alert ("your phone number is invalid! ");
Document. form. TEL. focus ();
Return false;
}
Return true;
}
-->
</Script>
2.4 form entry input value/length limit
Copy codeThe Code is as follows:
<Script language = "javascript">
<! --
Function CheckForm ()
{
If (document. form. count. value> 100 | document. form. count. value <1)
{
Alert ("the input value cannot be less than zero or greater than 100! ");
Document. form. count. focus ();
Return false;
}
If (document. form. MESSAGE. value. length <10)
{
Alert ("the input text is less than 10! ");
Document. form. MESSAGE. focus ();
Return false;
}
Return true;
}
// -->
</Script>
2.5 Determination of validity of Chinese/English/numbers/email addresses
Copy codeThe Code is as follows:
<Script language = "javascript">
<! --
Function isEnglish (name) // english value detection
{
If (name. length = 0)
Return false;
For (I = 0; I <name. length; I ++ ){
If (name. charCodeAt (I)> 128)
Return false;
}
Return true;
}
Function isChinese (name) // Chinese value detection
{
If (name. length = 0)
Return false;
For (I = 0; I <name. length; I ++ ){
If (name. charCodeAt (I)> 128)
Return true;
}
Return false;
}
Function isMail (name) // E-mail value detection
{
If (! IsEnglish (name ))
Return false;
I = name. indexOf ("");
J = name dot lastIndexOf ("");
If (I =-1)
Return false;
If (I! = J)
Return false;
If (I = name dot length)
Return false;
Return true;
}
Function isNumber (name) // value detection
{
If (name. length = 0)
Return false;
For (I = 0; I <name. length; I ++ ){
If (name. charAt (I) <"0" | name. charAt (I)> "9 ")
Return false;
}
Return true;
}
Function CheckForm ()
{
If (! IsMail (form. Email. value )){
Alert ("your email is invalid! ");
Form. Email. focus ();
Return false;
}
If (! IsEnglish (form. name. value )){
Alert ("the English name is invalid! ");
Form. name. focus ();
Return false;
}
If (! IsChinese (form. cnname. value )){
Alert ("invalid Chinese name! ");
Form. cnname. focus ();
Return false;
}
If (! IsNumber (form. PublicZipCode. value )){
Alert ("the zip code is invalid! ");
Form. PublicZipCode. focus ();
Return false;
}
Return true;
}
// -->
</SCRIPT>
2.6 restrict characters that cannot be entered for Form Items
Copy codeThe Code is as follows:
<Script language = "javascript">
<! --
Function contain (str, charset) // string contains the test function
{
Var I;
For (I = 0; I <charset. length; I ++)
If (str. indexOf (charset. charAt (I)> = 0)
Return true;
Return false;
}
Function CheckForm ()
{
If (contain (document. form. NAME. value, "% \ (\)> <") | (contain (document. form. MESSAGE. value, "% \ (\)> <")))
{
Alert ("illegal characters entered ");
Document. form. NAME. focus ();
Return false;
}
Return true;
}
// -->
</Script>
1. check whether a string is composed of digits.
---------------------------------------
<Script language = "Javascript"> <! --
Function checkNum (str) {return str. match (/\ D/) = null}
Alert (checkNum ("1232142141 "))
Alert (checkNum ("123214214a1 "))
// --> </Script>
2. How to determine whether it is a character
---------------------------------------
If (/[^ \ x00-\ xff]/g. test (s) alert ("containing Chinese characters ");
Else alert ("All characters ");
3. How to determine whether Chinese characters are contained
---------------------------------------
If (escape (str). indexOf ("% u ")! =-1) alert ("containing Chinese characters ");
Else alert ("All characters ");