2

Please see the mode below:

[RegularExpression("[0-9]", ErrorMessage = "Only numbers are allowed")]
        [Range(0D, 10000000000D, ErrorMessage = "Minimum salary must be 0")]
        [DisplayName("Salary")]
        public decimal Salary { get; set; }

and the view below:

Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })

If a user tries to enter a non-numeric then the number appears, however validation prevents the form from being submitted/

Websites like Confused.com have a Salary field when you are entering your details. The values entered into this field have to be numberic i.e. it is impossible to even enter a character or a minus sign i.e. if the user enters a non-character then it simply does not appear. How can I do this?

Also how can I add a £ sign to the textbox? This character should not be editable.

I have spent time looking into this. For example, I have read this question: How to force Razor to make Editorfor to input number type for float variable?

w0051977
  • 15,099
  • 32
  • 152
  • 329
  • I am no expert in `asp.net`, but aren't you supposed to have a key-down function (possible with `java script`), to control input in real time? – Jakob Busk Sørensen Oct 13 '17 at 09:26
  • Have you tried `[DisplayFormat(DataFormatString = "{0:C}")]` or `[DataType(DataType.Currency)]` for currency input like this issue: https://stackoverflow.com/questions/5080451/how-should-i-use-editorfor-in-mvc-for-a-currency-money-type? If you're not satisfied with `EditorFor`, use `TextBoxFor` (https://stackoverflow.com/questions/17834161/how-to-set-up-currency-format-for-mvc-editorfor). – Tetsuya Yamamoto Oct 13 '17 at 09:28
  • @Tetsuya Yamamoto, can you post some code. I have tried everything on those websites and the textboxes still accept letters. – w0051977 Oct 13 '17 at 09:34
  • Take a look at [this plugin](https://github.com/stephenmuecke/mvc-numericinput) –  Oct 13 '17 at 09:34

0 Answers0