2

I have a model and have the following code in it:

public class Student
{
public int StudentId { get; set; }

[Required(ErrorMessage = "*")]
[Range(0, 100, ErrorMessage = "Value must be less than 100")]
public int Score { get; set; }
}

I want to add another error message to the Score property. Right now, it displays the message 'value must be less than 100' if a value greater than 100 is entered. But i also want to add an error message that says "the entered value must be greater than 25. I understand that i can change the Range from 0-100 to 25-100, but is there a way i can display a different error message if a value below 25 is entered?

Dan
  • 63
  • 2
  • 9
  • Why not just "The value must be between 25 and 100"?. Otherwise you will need to write our own Minimum and Maximum attributes that derive from `ValidationAttribute` and inherit `IClientValidatable` –  Dec 21 '14 at 21:15

1 Answers1

0

You can do this:

<script>
        function CheckNumber() {

            var number = 50;
            var msg = '';
            var isValid = true;

            switch (true) {

                case (number > 60):
                    msg = "Greater than 60";
                    isValid = false;
                    break;

                case (number > 40):
                    msg = "Greater than 40";
                    isValid = false;
                    break;

                case (number > 20):
                    msg = "Greater than 20";
                    isValid = false;
                    break;
            }

            if (!isValid) {
                alert(msg);
                return false;
            }

            return true;
        }
    </script>
Eyal
  • 4,653
  • 9
  • 40
  • 56