Home > Web Front-end > JS Tutorial > body text

Detailed example of hiding email address with javascript

藏色散人
Release: 2021-08-13 14:49:09
Original
2606 people have browsed it

What if I don't want my email address to be viewed by unauthorized users? A good idea is here! In this article, I will introduce to you how to hide email addresses through javascript. do not miss it!

So let’s first describe the problem in detail: “How to write a JavaScript function to hide an email address from unauthorized users”?

The code goes directly below:

<!DOCTYPE html>
<html>
<head>
    <meta charset=utf-8 />
    <title></title>
</head>
<body>
<script>
    protect_email = function (user_email) {
        var avg, splitted, part1, part2;
        splitted = user_email.split("@");
        part1 = splitted[0];
        avg = part1.length / 2;
        part1 = part1.substring(0, (part1.length - avg));
        part2 = splitted[1];
        return part1 + "...@" + part2;
    };

    console.log(protect_email("robin_singh@example.com"));
</script>
</body>
</html>
Copy after login

We use console.log() to view the output information, as follows:

Detailed example of hiding email address with javascript

Normally hidden!

In the above js code, the methods and attributes that everyone needs to master are:

1, split()method

This method is used to To split a string into a string array, the syntax is "stringObject.split(separator,howmany)";

参数分别表示:
separator:字符串或正则表达式,从该参数指定的地方分割 stringObject。
howmany可选:该参数可指定返回的数组的最大长度。如果设置了该参数,返回的子串不会多于这个参数指定的数组。如果没有设置该参数,整个字符串都会被分割,不考虑它的长度。
Copy after login

2, length attributes can return strings The number of characters in , the syntax is "stringObject.length".

3, substring()Method

This method is used to extract the characters between two specified subscripts in the string. Its syntax is " stringObject.substring(start,stop)", its return value is a new string. The string value contains a substring of stringObject, and its content is all characters from start to stop-1, Its length is stop minus start.

参数分别表示:
start:一个非负的整数,规定要提取的子串的第一个字符在 stringObject 中的位置。
stop可选:一个非负的整数,比要提取的子串的最后一个字符在 stringObject 中的位置多 1。
注:如果省略该参数,那么返回的子串会一直到字符串的结尾。
Copy after login

Finally, I would like to recommend "JavaScript Basics Tutorial" ~ Welcome everyone to learn ~

The above is the detailed content of Detailed example of hiding email address with javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template