locked
How to check validation for numbersonly in data annotations RRS feed

  • Question

  • User269846090 posted

    How to check validation for numbersonly in data annotations?

    Friday, May 15, 2015 1:15 AM

Answers

  • User-821857111 posted

    You can use the Range validator:

    [Range(0, int.MaxValue, ErrorMessage="Numbers only, please")]

    Or you can use Regex:

    [RegularExpression(@"^\d+$", ErrorMessage="Numbers only, please")]



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 15, 2015 2:37 AM
  • User1577371250 posted

    Hi,

    Use this

    [Range(0, int.MaxValue, ErrorMessage = "Please enter valid Number")]
    public int Total { get; set; }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 15, 2015 2:38 AM

All replies

  • User-821857111 posted

    You can use the Range validator:

    [Range(0, int.MaxValue, ErrorMessage="Numbers only, please")]

    Or you can use Regex:

    [RegularExpression(@"^\d+$", ErrorMessage="Numbers only, please")]



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 15, 2015 2:37 AM
  • User1577371250 posted

    Hi,

    Use this

    [Range(0, int.MaxValue, ErrorMessage = "Please enter valid Number")]
    public int Total { get; set; }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 15, 2015 2:38 AM