Secure Social Security Number (SSN) Input Field showing only the final four digits | includes option to show/hide

I need to show only the last four digits of the SSN and mask the rest with asterisks, like: ****-**-7654

Additionally, I want to implement a toggle functionality where users can reveal or hide the masked characters behind the asterisks.

            <input type="password" name="password" id="password" />
            <i class="bi bi-eye-slash" id="togglePassword"></i>

Could you please assist me in resolving this issue?

Thank you for your help!

Answer №1

Although this solution may not meet all of your requirements, it will bring you very close to the desired outcome. The code utilizes blur() and focus() functions on the input element. Once you have entered your complete social security number, click outside the input field to observe the changes. Upon clicking the submit button, an alert will display the full social security number as entered.

$('form').on('submit', function(e){
    e.preventDefault();
    let socialSecurityValue = $('input[name=social]').data('value');
    alert('SS: ' + socialSecurityValue);
});

$('input[name=social]').focus(function(){
    let val = $(this).data('value');
    if (val) {
        $(this).val(val);
    }
});

$('input[name=social]').blur(function(){
    $(this).data('value', $(this).val());
    $(this).val( $(this).val().replace(/^\d{5}/, '***-**-'));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<form>
    <input class="social" type="text" name="social" maxlength="9" required>
    <input type="submit" value="Submit">
</form>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Generating an Xpath for the selected element with the help of Javascript or Jquery - here's how!

On my website, I have implemented a click event on an element. When a user clicks on this element, I want to generate the XPath of that particular element starting from either the html or body tag, also known as the Absolute Xpath. For example, if I click ...

What is the best way to overlay a video onto an image using HTML and CSS?

Currently, I am experimenting with a layering technique in HTML/CSS. The idea is to place an image behind a video to enhance the overall look of the video section. Here is a sample photoshop mockup that demonstrates what I am aiming for: HTML div id= " ...

How can I retrieve child divs from a cloned div object using jQuery?

I begin by executing this code to obtain a clone of my data var cloned = $(".hoverer:first").clone().attr({id: 'id_'+itm_id, class: 'hoverer-active'}); The main div contains another div with its own id and class, and within that div t ...

Developing JSON with the use of jQuery

My latest project involves creating an application that converts HTML tables to JSON format. The code is functioning properly, but I've encountered an issue when the td or th elements contain inner components like span or other child elements. While I ...

Maintain the layout of HTML page while reducing the window size

My webpage is experiencing an issue. When I resize the window, the placement of all elements becomes misaligned and the layout gets distorted. ...

Buttons with a slight lean towards the edge

The alignment of these buttons within their container is not what I desire. They are currently floating to the left, which is a result of the float: left; attribute applied to them. I tried removing the float: left; and instead used text-align: center; on ...

Managing the challenges of handling numerous AJAX post errors stemming from multiple form submissions

I am currently developing a PHP application that will be used to present multiple choice questions as well as text-based questions. The functionality I have implemented involves using Javascript code to submit user responses via ajax to the PHP server. He ...

Generate the URL based on the JSON feed

Can someone help me figure out what I'm doing wrong here? I'm attempting to create the image URL using the flickr.photos.search method now (I need to show images close to the visitor's geolocation), it was working with groups_pool.gne befor ...

Tips for implementing validation in AngularJS

Could someone help me with AngularJS validation? I'm new to it and trying to make sure everything is correct. var app=angular.module('myApp',[]) app.controller('myController',function($scope){ $scope.clickMe = function(){ if($(& ...

Modify the background color of the disabled checkbox in Vuetify

Check out this example where I found a disabled checkbox. Is there a way to set a custom background color for the "on" and "off" states of a disabled checkbox? ...

spontaneous angular rotations in a constantly shifting CSS environment

Is it possible to apply a random CSS rotation to an image just once, rather than having it continuously spin when hovering over a leaflet map? http://jsfiddle.net/J03rgPf/mzwwfav4/3/ I want the random CSS rotation to be set only one time. How can I achie ...

Avoid clicking on links while the webpage is still loading

I am facing an issue with my website where I need to intercept link-clicking events using jQuery. Everything works fine, but there is a problem if a user clicks on a link before JavaScript finishes loading, causing it to redirect to another page in error. ...

Best return type for a webservice triggered using jQuery but not requiring any data to be returned

I am currently working on a web service that utilizes several methods. These methods do not have significant impact on the client side as I call them using jQuery/ajax. My main concern is regarding the recommended return type. Typically, I would return JS ...

What is the best way to change the orientation of a scanner loop animation to

Currently, I have a CSS style featuring an animation that scans across a line in a loop. My goal is to apply this animation to a horizontal line, but I am struggling to figure out how to rotate the scanner for a horizontal loop. Below is my current code. A ...

The custom validation function in jQuery is not triggering

I am facing an issue with my HTML and JavaScript setup, which looks like this: <html> <head> <title>Validation Test</title> <script src="https://code.jquery.com/jquery-3.4.1.js"></script> <script src="htt ...

The carousel is not showing the images and content

I've been trying to create a 2-column layout with an image carousel on the left side and text on the right using Bootstrap-5. I have successfully implemented the text, but unfortunately, the carousel is not displaying. I have confirmed that I have lin ...

What is the process for utilizing bootstrap to automatically adjust font size on larger screens?

I'm facing an issue with displaying bullet points on varying screen sizes while using bootstrap. The fonts resize perfectly on my laptop, but when displayed on a large TV screen or tested with the responsive tool in the browser, the font size remains ...

What could be causing purgeCSS to strip away styles utilized in my React-Bootstrap application?

I have been attempting to clean up unused styles in my application, but the process of purging them seems to be removing even classes that are actually being used, resulting in a broken appearance on the site. The packages I am using include: "depe ...

Retrieve distinct ajax endpoint

I have a piece of code that rotates photos every few seconds using jQuery. Here is my jQuery code: setInterval("rotator();",4000); function rotator(){ var i = 0; var zdjecie = ''; $('#rotate').html(&apo ...

Teaching etiquette to the students of Li

I'm having trouble getting two lists to display correctly on my website. I want one main navigation list that is horizontal and another sub navigation list that is vertical. To achieve this, I need to have two different classes for the list items in m ...