1

Let's say I have a numeric password field, essentially a PIN:

[DataType(DataType.Password)]
public int Password { get; set; }

and then attempt to submit an alphabetic password, then the validation will fail on the server (as expected), but displays The value 'foo' is not valid for Password. to the user.

Is there any way to avoid displaying the attempted password? Or am I thinking about this wrong?

I could just make the field a string and validate it otherwise, but I'm not sure I like that solution, and was hoping there was something else I'm overlooking.

10
  • I could just make the field a string and validate it <-- i like it. Could you elaborate why you don't? to me it seems the in built data valiation doesnt do exactly what you want so you have to code something yourself - perfectly acceptable. Also given the password is not acceptable what is your issue with displaying it? Commented May 26, 2017 at 13:23
  • have you tried catching and handle the exception? Commented May 26, 2017 at 13:25
  • 1
    I would always take user Input as strings. If it need to be a Integer in the backend, you can parse it then. btb, only picking Numeric Characters as valid PW Characters means that it would be a heck of a lot easier to brute-force that PW anyway. Commented May 26, 2017 at 13:27
  • @wal so you have to code something yourself You're right, I'm just being stupid, but my issue with it wasn't not wanting to write it myself, rather that I was caught up on the fact that it's a number so it "should" be modeled as an int. Commented May 26, 2017 at 13:45
  • 1
    What about specifying an ErrorMessage with your data type attribute? [DataType(DataType.Password, ErrorMessage = "Not a valid password")] Commented May 26, 2017 at 13:45

1 Answer 1

1

It is possible to have a custom error message when using the DataType attribute.

[DataType(DataType.Password, ErrorMessage = "Your password must be a integer.")]

See documentation on this here.

As others have mentioned in the comments though, using an int may not be the best approach here.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm having trouble getting this to work, however. The error message isn't affected, as validation error isn't on the DataType attribute

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.