简体   繁体   中英

How to mask characters entered in a text input without failing model validation?

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; }

Use a password field with autocomplete="off".

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM