JS realizes the mobile phone number ID card and so on asterisking (*) number

Source: Internet
Author: User
Tags reserved strlen

Sometimes in order not to let the user's mobile phone number and ID card directly exposed to the page, we need to add an asterisk processing, the general situation is in the middle of the position with a few asterisks, the specific code can refer to the following:

var str= ' 13155555555 ';
var str2 = str.substr (0,3) + "* * *" +STR.SUBSTR (7); 131****5555

The simple method of intercepting and combining strings can be implemented, and we can encapsulate it into a method for later use

function plusxing (Str,frontlen,endlen) {
var len = Str.length-frontlen-endlen;
var xing = ';
for (Var i=0;i<len;i++) {
xing+= ' * ';
}
Return Str.substr (0,frontlen) +xing+str.substr (Str.length-endlen);
}

The meaning of three parameters: str: string, Frontlen: The number of reserved digits, Endlen: The number of reserved digits behind.

Above these writing can only cheat the child, like us this kind of programmer sees the source code to know the original mobile phone number, at this time we can use the server script to realize, like the PHP program, below looks an example.


Method One
function Mobile_asterisk ($mobile)
{
$mobile _asterisk = substr ($mobile, 0,4). " ". substr ($mobile, 8, 3);
return $mobile _asterisk;
}
Echo Mobile_asterisk ("15810904579");
Method Two
echo Preg_replace ("/(1\d{1,4}) \d\d\d\d (\d{3,4})/", "\$1****\$2", "15810904579");
Method Three
$haoma = "15012345678";
echo Preg_replace ("/(D{3}) d{5}/", "$1*****", $haoma);
Output 150*****678
Method Four
$tel 1 = "13888111188";
$tel 2 = "+8613888111188";
$tel 3 = "0861088111188";
$tel 4 = "086-010-88111188";
echo preg_replace ('/(^.*) \d{4} (\d{4}) $/', ' \\1****\\2 ', $tel 1), "\ n";
echo preg_replace ('/(^.*) \d{4} (\d{4}) $/', ' \\1****\\2 ', $tel 2), "\ n";
echo preg_replace ('/(^.*) \d{4} (\d{4}) $/', ' \\1****\\2 ', $tel 3), "\ n";
echo preg_replace ('/(^.*) \d{4} (\d{4}) $/', ' \\1****\\2 ', $tel 4), "\ n";
Method Five
Four digits in the middle of a masked phone number
function Hidtel ($phone)
{
$IsWhat = Preg_match ('/(0[0-9]{2,3}[\-]?[ 2-9][0-9]{6,7}[\-]? [0-9]?) /I ', $phone); Fixed telephone
if ($IsWhat = = 1)
{
return Preg_replace ('/(0[0-9]{2,3}[\-]?[ 2-9]) [0-9]{3,4} ([0-9]{3}[\-]?[ 0-9]?) /I ', ' $1****$2 ', $phone);
}
Else
{
return Preg_replace ('/(1[358]{1}[0-9]) [0-9]{4} ([0-9]{4})/I ', ' $1****$2 ', $phone);
}
}

Id

[Code]
echo strlen ($idcard) ==15?substr_replace ($idcard, "* * *", 8,4):(strlen ($idcard) ==18?substr_replace ($idcard, "* * *", 10,4): "ID number is not normal!" ");

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.