11

I am building a login page using ReactJS. I am using a custom text component built in-house, but unfortunately, it does not have a password masking option. Using the <input> tag with type=password is not an option. How can I make my password field appear masked (Dots instead of text) using Javascript/JQuery?

Is it a viable option to read each keydown event and replace the text with a dot while storing the char in a list or something on those lines?

1

3 Answers 3

29

Well, the way to achieve this is either in your JSX/HTML or by CSS for the most part.

To achieve this with JSX or HTML, you can use:

<input type="password" name="password">

To achieve this with CSS, you can use:

-webkit-text-security: disc;
text-security: disc;
Sign up to request clarification or add additional context in comments.

3 Comments

webkitTextSecurity: 'disc' does not seem to work. Is there an alternate in React?
WebkitTextSecurity: 'disc' worked (with a capital W). Thanks!
Notice that -moz-text-security is deprecated and should not be used in production envs.
6

I would simply condition the input type.

<input type={show_input?'text':'password'} 
       name='password' 
       id='password'
       value={user.password}
       onChange={handleChange}
 />

Change the value of show_input in your onChange function or add a button that changes shouw_input to true or false.

When type is password it will be masked, when it is text it will be unmasked.

Comments

3

You're looking for <input type="password">, which does exactly that.

5 Comments

From my question, "Using the <input> tag with type=password is not an option."
@saishreb: Why not? That's like asking "How can I insert a screw without a screwdriver?"
The custom text component that I am using for username field has been styled in a certain way which is difficult to reproduce with CSS. The password field needs to look exactly the same as the username field.
Can we see what the username field looks like? Without it we don't really have a reference to help you. Maybe you can mask the input field.
@SLaks when we want to limit who can screw the screw... to ask a user for their password and make sure to disable autocomplete (which autoComplete="off" is not guaranteed to work in all browsers) but we also want to mask the field using browser default behavior. Use case: User has a Delete Account button but we always want to make sure the user types the password and is not auto-completed by someone else that may sit on the users computer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.