Development Summary (2)-Javascript

Source: Internet
Author: User

This article is a summary of some of the Javascript I have encountered in my daily work, mainly for the use of scripts in Asp.net.
1. textbox verification (this is actually a regular content)

<! -- Use a regular expression to restrict Chinese characters only --> <input type = "text" onkeyup = "value = value. replace (/[^ \ u4e00-\ u9fa5]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[^ \ u4e00-\ u9fa5]/g, '')"/> <! -- Use a regular expression to limit that only numbers can be entered --> <input type = "text" onkeyup = "value = value. replace ([^ \ D]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[^ \ D]/g, '')"/> <! -- Use a regular expression to limit that only numbers and English characters can be entered --> <input type = "text" onkeyup = "value = value. replace ([\ W]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[^ \ D]/g, '')"/> <! -- Certificate number and small data point --> <input type = "text" id = "text1" onkeyup = "value = value. replace (/[^ \ d | ^ \.] /g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[^ \ d | ^ \.] /g, '')"> <SCRIPT type = "text/JavaScript"> // remove space function trim (STR) {return Str. replace (/(^ \ s *) | (\ s * $)/g ,"");} // The onbeforepaste attribute prevents copy and paste. // validate emailfunction Ismail (Mail) {var patrn =/^ W + ([-+.] W +) * @ w + ([-.] W + )*. W + ([-.] W +) * $/; return patrn. test (Mail);} // validate urlfunction isurl (URL) {var Regexp =/^ http: // [A-Za-z0-9] +. [A-Za-z0-9] + [/=? % -&_~ '@ []': +!] * ([^ <> ""]) * $/; Return Regexp. Test (URL) ;}</SCRIPT>

2. Check whether checkboxlist is selected.

// ID is the ID of the checkboxlist control. // return true indicates that function checkloccate (ID) {var status = true; var inputs = document is not selected. getelementbyid (ID ). getelementsbytagname ("input"); For (VAR I = 0; I <inputs. length; I ++) {If (inputs [I]. type = "checkbox" & inputs [I]. checked = true) {status = false;} return status ;}

3. Add items for dropdownlist

// ID is the idfunction addoptions (ID) {var DDL = document. getelementbyid (ID); // clears the content of the dropdownlist DDL. options. length = 0; For (VAR I = 0; I <10; I ++) {// options the first parameter is the text value, and the second parameter is the value DDL. add (New Option (I, I ));}}

4. Add rows and columns to the table
Adding rows and columns in Javascript is implemented through insertrow and inertcell. See the following example.

<HTML xmlns = "http://www.w3.org/1999/xhtml"> 

5. Differences between setTimeout and setinterval in Javascript

// Execute the handler function 1window. setinterval ("function 1", 1000) in one second; // delay 1 second to execute function 2window. settiemout ("function 2", 1000 );

6. JS: Get the length of the string

var str = str.replace(/[^\x00-\xff]/g, "**").length;

Updating ......

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.