Mask Mobile Number

In this example below you will see how to do a Mask Mobile Number with some HTML / CSS and Javascript

Here we write logic for masking of mobile number. Here in this code we write last 4 numbers as it is and rest of things will replace with X.

This awesome code was written by yogeshjadhav, you can see more from this user in the personal repository.
You can find the original code on
Copyright yogeshjadhav ©
  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

  <meta charset="UTF-8">
  <title>Mask Mobile Number</title>



    <script  src="js/index.js"></script>



/*Downloaded from */
var str = "12345678901"; 
var mobNum = str.slice(-4);
var restMobNum = str.substring(0, str.length - 7).replace(/\d/g, 'X'); 
console.log("Original Number: "+str);
console.log("Masked Number: "+restMobNum+ " XXX " +mobNum);
console.log("Masked Logic last 4 numbers will display as it is rest will convert into X.");