JavaScript Form Verification

Source: Internet
Author: User
Tags domain name registration to domain

<Script>
Function test ()
{
If (document. a. B. value. length> 50)
{
Alert ("cannot exceed 50 characters! ");
Document. a. B. focus ();
Return false;
}
}
</Script>
<Form>
<Textarea cols = "40" wrap = "VIRTUAL" rows = "6"> </textarea>
<Input value = "check">
</Form>

2. Only Chinese characters are allowed

<Input/oblog/value. replace (/[^ \ u4E00-\ u9FA5]/g, '')">

3. English only

<Script>
Function onlyEng ()
{
If (! (Event. keyCode> = 65 & event. keyCode <= 90 ))
Event. returnvalue = false;
}
</Script>

<Input>

4. Only numbers are allowed.

<Script>
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>

5. Only English characters and numbers are allowed.

<Input/oblog/value. replace (/[\ W]/g, "'') "onbeforepaste =" clipboardData. setData

('Text', clipboardData. getData ('text'). replace (/[^ \ d]/g, '')">

6. Verify the email format

<Script 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 (this. value)>

7. Shielding keywords (here, shielding ***** and ****)

<Script>
Function test (){
If (. b. value. indexOf ("***") = 0) | (. b. value. indexOf ("***") = 0 )){
Alert (":)");
A. B. focus ();
Return false ;}
}
</Script>
<Form>
<Input>
<Input value = "check">
</Form>

8. Whether the two passwords are the same

<Form method = post action = "">
<Input>
<Input>
<Input value = "test">
</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"

Add to body


II

2.1 Form items cannot be blank

<Script>
<! --
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

<Script>
<! --
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.

<Script>
<! --
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

<Script>
<! --
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

<SCRIPT>
<! --
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

<Script>
<! --
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> <! --
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 ");

4. Email format Verification
---------------------------------------
// Function name: chkemail
// Function Description: Check whether the Email Address is used
// Parameter description: the string to be checked
// Return value: 0: Not 1: Yes
Function chkemail ()
{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. digit format Verification
---------------------------------------
// Function name: fucCheckNUM
// Function Description: Check whether it is a number
// Parameter description: number to be checked
// Return value: 1 is a number, and 0 is not a number
Function fucCheckNUM (NUM)
{
Var I, j, strTemp;
StrTemp = "0123456789 ";
If (NUM. length = 0)
Return 0
For (I = 0; I <NUM. length; I ++)
{
J = strTemp. indexOf (NUM. charAt (I ));
If (j =-1)
{
// It indicates that the character is not a number.
Return 0;
}
}
// The description is a number.
Return 1;
}

6. Phone Number Format Verification
---------------------------------------
// Function name: fucCheckTEL
// Function Description: Check whether the phone number is used
// Parameter description: the string to be checked
// Return value: 1 is valid, 0 is invalid
Function fucCheckTEL (TEL)
{
Var I, j, strTemp;
StrTemp = "0123456789 -()#";
For (I = 0; I <TEL. length; I ++)
{
J = strTemp. indexOf (TEL. charAt (I ));
If (j =-1)
{
// The specified character is invalid.
Return 0;
}
}
// Valid description: www.2cto.com
Return 1;
}

7. functions that determine whether the input is Chinese
---------------------------------------
Function ischinese (s ){
Var ret = true;
For (var I = 0; I <s. length; I ++)
Ret = ret & (s. charCodeAt (I) >= 10000 );
Return ret;
}

8. Comprehensive function for determining the legality of user input
---------------------------------------
<Script>
// Limit the start of the number of characters
// M is the user input, and n is the number of digits to be limited
Function issmall (m, n)
{
If (m <n) & (m> 0 ))
{
Return (false );
}
Else
{Return (true );}
}

9. Determine if the password is consistent
---------------------------------------
Function issame (str1, str2)
{
If (str1 = str2)
{Return (true );}
Else
{Return (false );}
}

10. Determine whether the user name is a hyphen (/) with numbers or letters
---------------------------------------
Function notchinese (str ){
Var reg =/[^ A-Za-z0-9 _]/g
If (reg. test (str )){
Return (false );
} Else {
Return (true );}
}

11. Common verification functions for form text fields
---------------------------------------
Purpose: Check all input texts that must not be empty, such as names, accounts, and email addresses.
This verification is only applicable to text fields. If you want to target other domain objects in form, you can change the judgment conditions.

Usage: add the title text to the text field to be checked. The text is in the prompt information. You need to prompt the Chinese name of the field to the user. For example

User Name
Html is as follows <input title = "name">. Of course, you 'd better use a visualization tool such as dreamweaver to edit the domain.
If you want to detect data of the numeric type, the id of the domain is unified to sz.
It is difficult for javascript to judge the date type, so there is no program for date type verification.

Program comparison, just provide a train of thought. Let me know! :)
Oh, by the way, the function call method is: <form>

Function dovalidate ()
{
Fm = document. forms [0] // detects only one form. If there are multiple forms, the judgment conditions can be changed.
For (I = 0; I <fm. length; I ++)
{
// Detection and judgment conditions, which can be modified Based on Different Types
If (fm [I]. tagName. toUpperCase () = "INPUT" & fm [I]. type. toUpperCase () = "TEXT" & (fm [I]. title! = ""))

If (fm [I]. value = "/blog/= "")//
{
Str_warn1 = fm [I]. title + "cannot be blank! ";
Alert (str_warn1 );
Fm [I]. focus ();
Return false;
}
If (fm [I]. id. toUpperCase () = "SZ") // digit Verification
{
If (isNaN (fm [I]. value ))
{Str_warn2 = fm [I]. title + "Incorrect format ";
Alert (str_warn2 );
Fm [I]. focus ();
Return false;
}
}
}
Return true;
}


2> form submission Verification


2.1 Form items cannot be blank

<Script>
<! --
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

<Script>
<! --
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.

<Script>
<! --
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

<Script>
<! --
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

<SCRIPT>
<! --

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

<Script>
<! --

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>


From the treasure Valley

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.