Click here to Skip to main content
15,887,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to do validation for textboxes,
In my model I have two properties say

C#
[Required]
public int StartRange {get; set;}
[Required]
public int EndRange {get; set;}


In my view

C#
@Html.LabelFor(m => m.SignalStartRange, new { @class = "labelColor" }):
@Html.TextBoxFor(m => m.SignalStartRange, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.SignalStartRange, "", new { @class = "text-danger"})

@Html.LabelFor(m => m.SignalEndRange, new { @class = "labelColor" }) :
@Html.TextBoxFor(m => m.SignalEndRange, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.SignalEndRange, "", new { @class = "text-danger" })


I have to do validation that EndRange Should not be lesser than StartRange.

Please anyone help me how to do this.

What I have tried:

I was chechecking any data annotation attribute that will compare numbers.
Posted
Updated 9-Aug-17 23:22pm

I found out the solution, Here i created my custom attribute, [GreaterThan]
C#
[Required(ErrorMessage = "Signal Start Range Field Is Mandatory.")]  
        [DisplayName("Signal Start Range")]
        public string SignalStartRange { get; set; }

[Required(ErrorMessage = "Signal End Range Field Is Mandatory.")]
        [DisplayName("Signal End Range")]
        [GreaterThan(PropName = "SignalStartRange", ErrorMessage ="End rand should not be lesser than start ragne")]
        public string SignalEndRange { get; set; }



public class GreaterThanAttribute: ValidationAttribute
    {
        public string PropName { get; set; }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            PropertyInfo otherPropertyInfo = validationContext.ObjectType.GetProperty(PropName);

            var otherPropertyStringValue = otherPropertyInfo.GetValue(validationContext.ObjectInstance, null).ToString();

            if(Convert.ToUInt32(value) < Convert.ToUInt32(otherPropertyStringValue))
            {
                return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
            }
            return null;
        }
    }
 
Share this answer
 
v3
Comments
Amit Jha 2022 1-Apr-22 16:54pm    
How to do it in mvc core
You need to do client-side validation using javascript or jquery. Here is an example with a slider: Asp.Net MVC 5 with Jquery slider | The ASP.NET Forums[^] and here is a jquery dual range slider: jQRangeSlider: jQuery plugin for range sliders[^]

It should not be difficult to create a working solution from the above links.
 
Share this answer
 
Comments
Member 11859517 10-Aug-17 1:20am    
thanks, Is it relevant to my question?
i want two integer textboxes comparison. that last shoud not be lesser than first.
Graeme_Grant 10-Aug-17 3:17am    
I was recommending an alternative, that's is all.

But if you want to only use text entry, then you need to do custom client-side validation using javascript or jquery, not data annotation server side.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900