JavaScript and Regular Expressions

Source: Internet
Author: User
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 <input onkeyup = "value ="/oblog/value. replace (/[^ \ u4e00-\ u9fa5]/g, '')"> 3. "Only English <script language = JavaScript> function onlyeng () {If (! (Event. keycode> = 65 & event. keycode <= 90) event. returnvalue = false ;}</SCRIPT> <input onkeydown = "onlyeng ();"> 4. it can only be a number <script language = JavaScript> function onlynum () {If (! (Event. keycode> = 48 & event. keycode <= 57) | (event. keycode> = 96 & event. keycode <= 105) // consider the number key event on the keypad. returnvalue = false ;}</SCRIPT> <input onkeydown = "onlynum ();"> 5. only English characters and numbers <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; elsealert ("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 (":)");. 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. two Whether the password is 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. Right-click it and choose oncontextmenu = "Return false" ondragstart = "Return false" onselectstart = "Return false". In the body, the two 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, <script language = "JavaScript"> <! -- Function isnumber (string) {var letters = "1234567890-"; // you can add the value 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 item 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 Chinese/English/numbers/email address legality judgment <script language = "JavaScript"> <! -- Function isenglish (name) // check the English value {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 ("at"); j = Name dot lastindexof ("at"); 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 ("the Chinese name is invalid! "); 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 ("an invalid character is 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 a Chinese character contains ----------------------------------- 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 it is email address // parameter description: the string to be checked // return value: 0: Not 1: is function chkemail (a) {var I =. length; var temp =. indexof ('@'); var tempd =. indexof ('. '); If (temp> 1) {If (I-Temp)> 3) {If (I-tempd)> 0) {return 1 ;}}} return 0;} 5. digit format verification --------------------------------------- // function name: fuccheckn Um // Function Description: Check whether it is a number // parameter description: number to be checked // return value: 1 is a number, 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) {// 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 a valid parameter description: the string to be checked // return value: 1, 0 is invalid function f Ucchecktel (TEL) {var I, j, strtemp; strtemp = "0123456789-() #"; for (I = 0; I <Tel. length; I ++) {J = strtemp. indexof (tel. charat (I); If (j =-1) {// The return 0;} // The return 1;} 7. function --------------------------------------------------- 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"> // restrict the start of the number of digits of the input character. // M indicates the user input, and N indicates the maximum number of digits. 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 =/[^ -Za-z0-9 _]/G if (Reg. test (STR) {return (false) ;}else {return (true) ;}} 2.8. common verification function ----------------------------------------------- of form text field: checks all input texts that must not be blank, such as names, accounts, email addresses, and so on. 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, if you want to check the username HTML as follows <input name = "txt_1" Title = "name">, you 'd better use a visualization tool such as Dreamweaver to edit the domain. If you want to check the data of the numeric type, it is troublesome to unify the domain ID into Sz. 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 ()"> function dovalidate () {fm = document. forms [0] // detects only one form. If there are multiple forms, you can change the judgment condition for (I = 0; I <FM. length; I ++) {// check the judgment condition. You can modify if (FM [I] based on the type. 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") // number verification {If (isnan (FM [I]. value) {str_warn2 = FM [I]. title + "incorrectly formatted"; alert (str_warn2); FM [I]. focus (); Return false ;}}return true ;}
0

| Comment

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.