JQuery implements the method for adding specific content to a string according to the specified length. jquery string

Source: Internet
Author: User

JQuery implements the method for adding specific content to a string according to the specified length. jquery string

This example describes how jQuery adds a specified length to a string. Share it with you for your reference. The specific analysis is as follows:

In a recent project, mobile phone numbers must be separated by identifiers for easy reading. After searching for a mobile phone number on the Internet and finding no proper code, I wrote a function by myself, you can insert delimiters to a string based on the specified length.

Var split_str = false; function insert_flg (str, flg, sn) {str = str. replace (new RegExp (flg, "g"), ""); var newstr = ""; var tmp; var len = str. length; // length var num = len/sn; // number of segments var start; var end; // len % sn // whether the complete segments are 0: yes for (I = 0; I <num; I + = 1) {if (len % sn! = 0) {// incomplete segment start = I * sn-1; end = I * sn + (sn-1);} else {start = I * sn; end = (I + 1) * sn;} start = start <0? 0: start; if (end <= len) {tmp = str. substring (start, end);} newstr + = (end> = len )? Tmp: tmp + flg;} split_str = newstr; return newstr ;}$ (function () {var phone =$ ("# phone"); phone. blur (function () {// triggers var cont = phone when the focus is lost. val (); cont = jQuery. trim (cont); var str_p = '-'; // shard symbol var s = 4; // if (! Cont | split_str = cont) return false; // when the focus leaves again, check whether the content has changed. phone. val (insert_flg (cont, str_p, s ));})})

I hope this article will help you with jQuery programming.

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.