0

How do you emulate the behavior of the password field without using @Html.PasswordFor. In other words, I want to keep it as a text field (@Html.EditorFor), but show a masked character (*) instead of the typed-in character. The reason I don't want to use a password field is because this is not actually for a password, but for some other sensitive data, and using a password field means Chrome prompts the user to save the password.

I know I could use a javascript using onkeyup to convert the typed-in character to a masked character and save the actual character in a hidden field, but the problem is that the model validation on this field would reject that character.

What is the best way to approach this problem?

View

 @Html.EditorFor(model => model.Salary)

Model

[RegularExpression("([1-9][0-9]*)")] //model validation would reject asterisks.
public string Salary { get; set; }
Prabhu
  • 11,837
  • 31
  • 115
  • 194
  • 2
    Have you tried ? – Blue May 06 '16 at 17:48
  • @Blue, Yes, as mentioned on the first line of the question. Html.PasswordFor is basically – Prabhu May 06 '16 at 18:28
  • You can try making it a text input and changing the font language to look like a password using css. It depends on what fonts you have available but is more maintainable/responsive than js to do it. Check this thread http://stackoverflow.com/questions/6859727/styling-password-fields-in-css. – Blue May 06 '16 at 20:52
  • @Blue, Ok, I think something like that is what I was looking for. – Prabhu May 07 '16 at 00:07

1 Answers1

2

Use a password field with autocomplete="off".

See Chrome Browser Ignoring AutoComplete=Off and Disable browser 'Save Password' functionality.

Community
  • 1
  • 1
Feathercrown
  • 1,886
  • 1
  • 13
  • 23