Use javascript to verify the mobile phone number and implement asterisks (*) to replace the instance, and use javascript asterisks

Source: Internet
Author: User

Use javascript to verify the mobile phone number and implement asterisks (*) to replace the instance, and use javascript asterisks

I. use JavaScript to replace four digits in the middle of the mobile phone number

// Match the beginning and end of the mobile phone number, and output '123' in a format similar to "123 *** 8901 '. replace (/(\ d {3}) \ d {4} (\ d {4})/, '$1 ***** $2 ');

Example

<! Doctype html> 

Note:This regular expression matches 11 consecutive numbers in the string, replacing the four digits in the middle with the "*" character, and output the common format of the hidden mobile phone number. If you want to get only the last four digits, you can change it to the following format:

2. Use JavaScript to replace the first seven digits of the mobile phone number

// Match 11 consecutive digits, and replace the first seven digits with the number * '123 '. replace (/\ d {7} (\ d {4})/, '******** $1 ');

Example

<! Doctype html> 

Additional Notes:Parentheses in the regular expression can be used for grouping and defining sub-pattern strings. In the replace () method, $ n (n is a number) can be used in parameter 2) to reference strings defined in parentheses in a pattern string in sequence.

Iii. JavaScriptMobile phone verification and hidden four-digit comprehensive example of mobile phone number

<! Doctype html> 

Summary

The above is all the content of the javascript verification mobile phone number and the implementation of asterisk (*) to replace the effect, I hope the content of this article will be helpful for your daily use of JavaScript.

Related Article

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.