Keywords: JS verification form Daquan, use js to control form submission, JavaScript to submit form:

Source: Internet
Author: User
Tags domain name registration to domain
Directory:
1: JS string length limit, determination character length, JS restriction input, restriction not input, textarea length limit
2.: JS judges Chinese characters, determines whether Chinese characters are needed, and can only input Chinese Characters
3: JS determines whether the input is in English. Only English can be entered.
4: javascript can only enter numbers, determine numbers, verify numbers, detect numbers, determine whether they are numbers, and only enter numbers
5: Only English characters and numbers can be entered
6: JS email verification, JS judgment email, mailbox/mailbox format Verification
7: JS character filtering, blocking keywords
8: JS password verification and password Determination
2.1: JS is not empty, empty, or not an object. The judgment is empty. The judgment is not empty.
2.2: compare whether the values of two form items are the same
2.3: The form can only contain numbers and "_",
2.4: Number/length limit entered for Form Items
2.5: Chinese/English/number/email address legality judgment
2.6: Restrict characters that cannot be entered for Form Items
2.7 form User Control
2.8: Common verification functions for form text fields

1. length limit

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. "can only be English

Script Language = Javascript
Function onlyeng ()
{
If (! (Event. keycode = 65 & event. keycode = 90 ))
Event. returnvalue = false;
}
/Script

Input onkeydown = "onlyeng ();"
4. Only numbers are allowed.

Script Language = Javascript
Function onlynum ()
{
If (! (Event. keycode = 48 & event. keycode = 57) wsws (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.

Input onkeyup = "value ="/oblog/value. replace (/[\ W]/g, ")" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[^ \ D]/g, ")"

6. Verify the email format

Script Language = JavaScript runat = Server
Function isemail (stremail ){
If (stremail. search (/^ \ W + (-\ W +) WS (\. \ W +) * \ @ [A-Za-z0-9] + ((\. WS-) [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 ****)

Script Language = "javascript1.2"
Function Test (){
If (. b. value. indexof ("***") = 0) wsws (. 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

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

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

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.

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 illegal !");
Document. Form. Tel. Focus ();
Return false;
}
Return true;
}
-"
/Script

2.4 form entry input value/length limit

Script Language = "JavaScript"
《! -
Function checkform ()
{
If (document. Form. Count. Value 100 wsws 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 ("Input text less than 10 !");
Document. Form. Message. Focus ();
Return false;
}
Return true;
}
//-"
/Script

2.5 Determination of validity of Chinese/English/numbers/email addresses

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" wsws 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 ("invalid English name !");
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 postal code is invalid !");
Form. publiczipcode. Focus ();
Return false;
}
Return true;
}
//-"
/Script

2.6 restrict characters that cannot be entered for Form Items

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, "% \ (\)") wsws (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 ");

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
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 Language = "JavaScript"
// 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 );}
}
2.8. 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, to check the user name
HTML is as follows: "input name =" txt_1 "Title =" name ". Of course, it is best to 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:

Form onsubmit = "Return dovalidate ()"

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" & amp; 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 + "The format is incorrect ";
Alert (str_warn2 );
FM [I]. Focus ();
Return false;
}
}
}
Return true;
}

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.