Javascript Form Verification

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

1. Only numbers and English letters can be entered:
<Input onkeyup = "value = value. replace (/[/W]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^/d]/g, '')" ID = "Text1" NAME = "Text1">
2. Only numbers can be entered:
<Input onkeyup = "value = value. replace (/[^/d]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^/d]/g, '')" ID = "Text2" NAME = "Text2">
3. Only the fullwidth fields can be entered:
<Input onkeyup = "value = value. replace (/[^/uFF00-/uFFFF]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^/uFF00-/uFFFF]/g, '')" ID = "Text3" NAME = "Text3">
4. Only Chinese characters can be entered:
<Input onkeyup = "value = value. replace (/[^/u4E00-/u9FA5]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^/u4E00-/u9FA5]/g, '')" ID = "Text4" NAME = "Text4">
--------------------------------------------------------------------------------
Verify invalid characters, SQL
Function vdf ()
{
Var I, j, name, value, message, length, type, a = vdf. arguments, cb_name;
For (I = 0; I <(a. length-2); I + = 3)
{
If (a [I]. indexOf ('#')! =-1)
{
Name = fob (a [I]. substr (0, a [I]. indexOf ('#')));
Cb_name = fob (a [I]. substr (a [I]. indexOf ('#') + 1), a [I]. length ));
}
Else
{
Name = fob (a [I]); // Control name
}
Message = a [I + 1]; // prompt message
Type = a [I + 2]; // type
If (type! = "R_time ")
{
Value = name. value. replace (/+/g, ""); // Control value
}
Else
{
Value = name. value;
}
}
--------------------------------------------------------------------------------
// ======================== Determine whether the check box is selected ============================== //
If (type = "r_cb ")
{
E = document. forms (0). elements;
Var flag = false;
For (I = 0; I <e. length; I ++)
{
If (e [I]! = Cb_name)
{
If (e [I]. checked = true)
{
Flag = true;
Break;
}
}
If (I = e. length-1)
{
Break;
}
}
If (flag = false)
{
Alert (message + "! /N "); // The prompt when it is null
Return false;
}
}
// ======================== Determine whether to select the drop-down box ============================ //
If (type = "r_sl ")
{
If (name. selected = false)
{
Alert (message + "! /N "); // The prompt when it is null
Return false;
}
}
// ========================== Cannot be empty //
If (type = "r ")
{
If (value = "") // determines whether it is null
{
Alert (message + "! /N "); // The prompt when it is null
Name. focus ();
Name. select ();
Return false;
}
}
// ======================== Cannot be empty, but do not get focus ============================ //
If (type = "o_r ")
{
If (value = "") // determines whether it is null
{
Alert (message + "! /N "); // The prompt when it is null
Return false;
}
}
// ======================== Only Chinese characters can be entered ============================== //
If (type = "r_china ")
{
If (value. search (/^ [/u4e00-/u9fa5] + $/) =-1)
{
Alert (message + "! /N "); // cannot be empty
Name. focus ();
Name. select ();
Return false;
}
}
// ======================== Cannot be blank, must be a number or character ============================== //
If (type = "r_num_char ")
{
If (value = "")
{
Alert (message + "! /N "); // The prompt when it is null
Name. focus ();
Name. select ();
Return false;
}
If (value. search (/^ [0-9a-zA-Z] + $/) =-1)
{
Alert (message + "! /N "); // The prompt when it is null
Name. focus ();
Name. select ();
Return false;
}
}
// ======================== Can be empty, not empty, enter a number ============================ //
If (type = "num ")
{
If (value. search (/^ [0-9] + $/) =-1 & value! = "")
{
Alert (message + "! /N "); // cannot be empty
Name. focus ();
Name. select ();
Return false;
}
}
// ======================== Cannot be blank, must be a number to determine ============================ //
If (type = "r_num ")
{
If (value = "")
{
Alert (message + "! /N ");
Name. focus ();
Name. select ();
Return false;
}
If (value. search (/^ [0-9] + $/) =-1)
{
Alert (message + "! /N "); // cannot be empty
Name. focus ();
Name. select ();
Return false;
}
}
// ========================== Numbers smaller than n must be entered//
If (type. indexOf ("r_num <")! =-1)
{
Length = type. substring (type. indexOf ('<') + 1), type. length); // obtain the number following rn <
If (value = "") // prompt for empty
{
Alert (message + "! /N ");
Name. focus ();
Name. select ();
Return false;
}
If (value. search (/^ [0-9] + $/) =-1) // The prompt is not composed of numbers.
{
Alert (message + "! /N ");
Name. focus ();
Name. select ();
Return false;
}
If (value. length> length) // limits the number length.
{
Alert (message + "! /N ");
Name. focus ();
Name. select ();
Return false;
}
}
// ========================== Numbers greater than n must be input//
If (type. indexOf ("r_num> ")! =-1)
{
Length = type. substring (type. indexOf ('>') + 1), type. length); // obtain the number after rn <
If (value = "") // prompt for empty
{
Alert (message + "! /N ");
Name. focus ();
Name. select ();
Return false;
}
If (value. search (/^ [0-9] + $/) =-1) // The prompt is not composed of numbers.
{
Alert (message + "! /N ");
Name. focus ();
Name. select ();
Return false;
}
If (value. length <length) // limits the number length
{
Alert (message + "! /N ");
Name. focus ();
Name. select ();
Return false;
}
}
// ========================== Numbers between a and B must be entered == //
If (type. indexOf ("r_num # <> ")! =-1)
{
Length = type. substr (type. indexOf ('>') + 1), type. length );
Length = length. substr (0, length. lastIndexOf ("-"));
Length1 = type. substring (type. indexOf ('-') + 1), type. length) // obtain the number after rn <
If (value = "") // prompt for empty
{
Alert (message + "! /N ");
Name. focus ();
Name. select ();
Return false;
}
If (value. search (/^ [0-9] + $/) =-1) // The prompt is not composed of numbers.
{
Alert (message + "! /N ");
Name. focus ();
Name. select ();
Return false;
}
If (value. length <length | value. length> length1) // limits the length of a number.
{
Alert (message + "! /N ");
Name. focus ();
Name. select ();
Return false;
}
}
// ======================== Cannot be blank, it must be of the float type ========================== //
If (type = "r_float ")
{
If (value = "")
{
Alert (message + "! /N ");
Name. focus ();
Name. select ();
Return false;
}
If (value. search (/^ [0-9] + $ /)! =-1 | value. search (/^ ([0-9] +) | ([0-9] + /. [0-9] *) | ([0-9] */. [0-9] +) $ /)! =-1)
{
Return true;
}
Else
{
Alert (message + "! /N "); // cannot be empty
Name. focus ();
Name. select ();
Return false;
}
}
// ====================== Determine email, not necessarily input ============================== //
If (type. indexOf ("email ")! =-1)
{
If (name. value! = "")
{
If (value. search (/^ [_/. a-z0-9] + @ [a-z0-9] + [/.] [a-z0-9] {2,} $/I) =-1)
{
Alert (message + "! /N ");
Name. focus ();
Name. select ();
Return false;
}
}
}
// ====================== To determine the email, enter ================================== //
If (type. indexOf ("r_email ")! =-1)
{
If (name. value = "")
{
Alert (message + "! /N ");
Name. focus ();
Name. select ();
Return false;
}
If (value. search (/^ [_/. a-z0-9] + @ [a-z0-9] + [/.] [a-z0-9] {2,} $/I) =-1)
{
Alert (message + "! /N ");
Name. focus ();
Name. select ();
Return false;
}
}
// ====================== Judge the date, for example, 2000-12-20 ========================== //
If (type = "r_date ")
{
Flag = true;
Getdate = value;
If (getdate. search (/^ [0-9] {4}-(0 [1-9] | [1-9] | 1 [0-2]) -(0 [1-9] | [1-9]) | 1 [0-9] | 2 [0-9] | 3 [0-1]) $/) =-1) // The input format is correct.
{
Flag = false;
}
Else
{
Var year = getdate. substr (0, getdate. indexOf ('-') // obtain the year
// The following operations get the month
Var transition_month = getdate. substr (0, getdate. lastIndexOf ('-'));
Var month = transition_month.substr (transition_month.lastIndexOf ('-') + 1, transition_month.length );
If (month. indexOf ('0') = 0)
{
Month = month. substr (1, month. length );
}
// The following operation gets the date
Var day = getdate. substr (getdate. lastIndexOf ('-') + 1, getdate. length );
If (day. indexOf ('0') = 0)
{
Day = day. substr (1, day. length );
}
// Alert (month );
// Alert (day)
// Return false;
If (month = 4 | month = 6 | month = 9 | month = 11) & (day> 30) // 4,6, the month date cannot exceed 30
{
Flag = false;
}
If (month = 2) // judge January
{
If (LeapYear (year ))
{
If (day> 29 | day <1) {flag = false ;}
}
Else
{
If (day> 28 | day <1) {flag = false ;}
}
}
Else
{
Flag = true;
}
}
If (flag = false)
{
Alert (message + "! /N "); // The prompt when it is null
Name. focus ();
Name. select ();
Return false;
}
}
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. "English only
<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) | (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 fuel tank format
<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 ****)
<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
<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 invalid! ");
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 | 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 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
<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 ");
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, 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, right. function call method: <form onsubmit = "return dovalidate ()">
Copy codeThe Code is as follows:
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;
}

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.