Replace text with jQuery asterisk

Asked

Viewed 1,622 times

2

I have the following field:

<input type="number" name="teste_senha">

I need that, when typing some content, this content is replaced by ****, without losing its original content, can be stored in a field Hidden.

How do I get this result?

  • you’ve already tried something?

  • No. Not yet

  • but you want to hide and show this content through a user action?

  • Exactly, actually, my idea is in the mobile version, in the password field, accept only NUMBERS, then I thought to do so, mount as type number, and mask when typing...

  • 1

    As I understand it, you want to replace the characters with **** while the user fills in the field, correct? if yes, you have to create an Hidden input yourself, and use the "change" javascript event to see changes in the input, or use the keyup

  • 3

    No, you don’t have to use the hidden. If the field is a password, use the type password and validate with JS. What you’re asking for is gambiarra. Read about the attribute pattern of HTML 5.

Show 1 more comment

1 answer

5


As they said, it doesn’t make much sense what you want, is to have more work for nothing. HTML already has its own attribute to hide passwords with asterisks: type="password".

What you can do if you just want numbers on input, is to do a validation via Javascript. Using regex would be an option:

function validar(){
   if( /^[0-9]*$/.test($("#campo").val()) && $("#campo").val()){
      alert("Ok!");
   }else{
      alert("Erro! Só são permitidos números");
   }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="campo" type="password" name="teste_senha">
<br />
<input type="button" value="Verificar" onclick="validar()">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.