Range validator in MAUI Blazor

Kalyan A 285 Reputation points
2024-09-05T09:48:22.2233333+00:00

Here is a simple app to Add numbers from 0 to 100 , after which I like to perform some statistical operations.

Add Insert works fine , I can restrict only 0 to 100

Delete works fine.

When editing it is accepting all numbers , I like to restrict 0 to 100,please suggest.I am looking something like a range validator to restrict my input from 0 to 100.

@page "/Range"
 
<PageTitle>Probability</PageTitle>
 
<ul>
    @foreach (var todo in todos)
    {
        <li>
            <input type="checkbox" @bind="todo.IsDone" />
            <input   @bind="todo.Title"  />
        </li>
    }
</ul
<ul>
    @foreach (var todo in listsort)
    {
        <li>
            
            <input readonly @bind="todo.Title" />
        </li>
    }
</ul>
<input placeholder="Something todo" @bind="newTodo" />
<button @onclick="AddTodo">Add</button> <br>
<button @onclick="DeleteTodo">Delete</button>
<p>To Delete Check the Box </p>
<div>
    Range:
  </div>
<div>
    @ran
    </div>
<div>
        @errmsg
    </div>
@code {
    public class TodoItem
    {
        public int Title { get; set; }
        public bool IsDone { get; set; }
    }
    private List<TodoItem> todos = new();
    private List<TodoItem> listsort = new();
    private int newTodo; int rancnt;
    private string errmsg;private string skip; private int max;private int ran;
    private void DeleteTodo()
    {
        int rem = todos.Count((s => s.IsDone == true));
        if (rem > 0)
        {
            var toRemove = todos.Where(todos => todos.IsDone == true).ToList();  
            foreach (var s in toRemove)
            {
                todos.Remove(s);
            }
        }
        listsort = todos.OrderBy(p => p.Title).ToList();
          rancnt = todos.Count();
         if (rancnt >0)
         { 
        ran =   todos.Max(p => p.Title)-todos.Min(p => p.Title) ;
         }
    }
    private void AddTodo()
    {errmsg = "";
        skip = "N";
        errmsg = "";
        if( (newTodo <= 0) ||   (newTodo > 100)) 
        { skip = "Y"; 
        errmsg = "Enter number between 0 and 100";}
        
   //   int cnt  = todos.Where(todos => todos.Title ==newTodo).Count();
    //  if (cnt>0)
    // {  
   //  errmsg = "Number already exists";}
       
  if(skip=="N")
  { 
        todos.Add(new TodoItem { Title = newTodo });
  }
        newTodo = 0;
        listsort = todos.OrderBy(p => p.Title).ToList(); 
        rancnt = todos.Count();
         if (rancnt >0)
         { 
        ran =   todos.Max(p => p.Title)-todos.Min(p => p.Title) ;
         }
    }
    
    
}
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,594 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,578 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Kalyan A 285 Reputation points
    2024-09-06T13:23:45.85+00:00

    As said By AgaveJoe I will try to capture the validation during Submit.

    @page "/Range"
     
    @using System.ComponentModel.DataAnnotations;  
     
      
    <PageTitle>Probability</PageTitle>
     
    <ul>
        @foreach (var todo in todos)
        {
            <li>
                <input type="checkbox"  @bind="todo.IsDone" />
                <input  readonly @bind="todo.Title"  />
            </li>
        }
    </ul
    <input placeholder="Something todo" @bind="newTodo" />
    <button @onclick="AddTodo">Add</button> <br>
    <button @onclick="DeleteTodo">Delete</button>
      <div>
       Ascending Order  
      </div>
     <ol>
        @foreach (var todo in listsort)
        {
            <li>
                
                <input readonly @bind="todo.Title" />
            </li>
        }
    </ol>
    <button @onclick="Submit">Perform Calc</button>
    <div>
        Range:
      </div>
    <div>
        @ran
        </div>
          <br />
        <div>
        Count:
      </div>
    <div>
        @rancnt
        </div>
     
             <br />
        <div>
        Sum:
      </div>
    <div>
        @meansum
        </div>
             <br />
        <div>
        Mean:
      </div>
    <div>
        @calcmean
        </div>
             <br />
      <div>
        Mode(s)
      </div>  
      
      <div>
      <ul>
         
        @for  (int i = 0; i < myNum.Length; i++) 
        {
                <li>
                    @if(display=="Y")
                    { 
                   @myNum[i] 
                    }
                 
                </li>
        }
           
    </ul>
     </div>  
      <br />
      <div>
          Median
          </div>  
          <div>
        @calcmedian
        </div>
    <div>
      
            @errmsg
        </div>
     
    @code {
        public class TodoItem
        {
            public int Title { get; set; }
            public bool IsDone { get; set; }
        }
        public class  Groups
        {
            public int Title { get; set; }
        }
        public class  Summary
        {
            public int Title { get; set; }
            public int cnts { get; set; }
        }
        private List<TodoItem> todos = new();
        private List<TodoItem> listsort = new();
        private int newTodo; int rancnt; int meansum;
        private string errmsg;private string skip; private int max;private int ran; private decimal calcmean; private decimal calcmedian; int median = 0; private int[] myNum = { 0  }   ;
        private List<Groups> modegroups = new();
        private List<Groups> tempgroups = new(); private string display = "N"; private int[] medarr; 
        private List<Summary> sumgroups = new();
        protected override void OnInitialized()
        {
        }
        private void DeleteTodo()
        {
            display = "N";  
            ran = 0;
            rancnt = 0;meansum = 0; calcmean = 0;calcmedian = 0;
            int rem = todos.Count((s => s.IsDone == true));
            if (rem > 0)
            {
                var toRemove = todos.Where(todos => todos.IsDone == true).ToList();  
                foreach (var s in toRemove)
                {
                    todos.Remove(s);
                }
            }
            listsort = todos.OrderBy(p => p.Title).ToList();
        }
        private void AddTodo()
        {     display = "N";
            myNum[0] = 0;
            ran = 0;
            rancnt = 0;meansum = 0;calcmean = 0;calcmedian = 0;
            errmsg = "";
            skip = "N";
            errmsg = "";
            if( (newTodo <  0) ||   (newTodo > 100)) 
            { skip = "Y"; 
                errmsg = "Enter number between 0 and 100";}
            int cnt  = todos.Count();
            if (cnt>20)
            {   skip = "Y"; 
                errmsg = "Cannot enter number more than 20 numbers";}
            if(skip=="N")
            { 
                todos.Add(new TodoItem { Title = newTodo });
            }
            newTodo = 0;
            listsort = todos.OrderBy(p => p.Title).ToList(); 
        }
        private void Submit()
        { display = "Y";
            meansum = todos.Sum(p => p.Title) ;
            rancnt = todos.Count();
            if (rancnt >0)
            { 
                ran =   todos.Max(p => p.Title)-todos.Min(p => p.Title) ;
                calcmean = Math.Round( Decimal.Divide(meansum ,  rancnt),2)  ;
                //
                var tempgroups =listsort.GroupBy(n => n.Title)
                                 .Select(n => new
                                 {
                                     Title = n.Key,
                                     TitleCount = n.Count()
                                 })
                                 .OrderBy(n => n.Title);
              //  sumgroups = tempgroups;
                int groupmax = tempgroups.Max(p => p. TitleCount );
                var   modegroup  =tempgroups.Where(groups => groups.TitleCount == groupmax).Select(n =>  n.Title ).ToList();
                myNum =   modegroup.ToArray();
                var   medgroup  =listsort.Select(n =>  n.Title ).ToList();
                medarr = medgroup.ToArray();
                int size = medarr.Length;
                int mid = size/2;
                // int median = 0;
                if ((size % 2) != 0){
                    calcmedian =  medarr[mid];
                }
                else {
                    int val1 = medarr[mid - 1];
                    int val2 = medarr[mid];
                    int val3 = val1 + val2;
                    calcmedian = Decimal.Divide(val3,2);
                    calcmedian = Math.Round(calcmedian, 2);
                }
             }}
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.