Ultra-practical JavaScript form code snippet _ javascript skills

Source: Internet
Author: User
This article mainly Shares Ultra-practical JavaScript form code segments to help you better learn how to use javascript form operations, interested friends can refer to the useful Javascript form code snippet and share it with you for your reference. The specific content is as follows:

1. Multiple window. onload Methods

The onload method is automatically called after the page is loaded. Therefore, it is widely used, but the disadvantage is that onload can only be used to execute one method. The following code segment ensures that multiple methods are executed during Onload:

 function addLoadEvent(func){  var oldonload = window.onload;  if(typeof window.onload != 'function'){   window.onload = func;  }else{   window.onload = function(){    oldonload();    func();   }  } }

2. Use a regular expression to remove spaces.

str.replace(/^(\s|\u00A0)+|(\s|\u00A0)+$/g,"");

3. Use regular expressions to filter Chinese Characters

str.replace(/[\u4e00-\u9fa5]/g,"");

4. Prohibit copying and copying.

xxx.oncopy = function(){ return false;}xxx.onpaste = function(){ return false;}

5 limit the length of a string (distinguish Chinese and English)

Main ideas:

Three data records are required: 1. The length of the input is limited; 2. The length of the input is limited; 3. The number of characters intercepted;

Because the interception methods in JS do not distinguish between Chinese and English

"HAHAHA". substr () ----> haha

"Haha". substr (0, 2) ---> ha

However, if a Chinese character is encoded, it must be 2 characters, and a letter must be 1 character.

Therefore, when calculating the actual length, it should be the length of characters + the number of Chinese Characters

For example, if we enter a limit of five characters: After "Haha" is entered, only one h can be entered, and no more Chinese characters can be entered. For more information about the code, run the following command.

 

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.