How to validate a texbox that only lets me enter numbers up to 100

2

How can I validate a textbox so that it does not allow me to enter more than 100 in asp.net?

Try doing that:

 <%= Html.TextBox("nota" + cont, item.nota, new { @class = "form-control", type = "number", min = "0", max = "100", id = "txtNota", placeholder = "0" , onkeypress = "ValidarNumero(event)"})%>  



<script type="text/javascript">   
 document.addEventListener('keypress', ValidarNumero);        
    function ValidarNumero(numero) {                                                   
    if ((event.keyCode < 100) || (event.keyCode > 0))
       event.returnValue = true;
    else
       event.returnValue = false;
 }                  
</script>

but I only get the key entered and not the 2 or 3 keys that are entered ... help by fa

    
asked by Romer Ariel Romero Isnado 02.05.2018 в 15:35
source

2 answers

1

For what I interpreted the validation you want to do from the client side, it would be easy to do it by adding a EventListener to input

For example;

document.getElementById ("txtNota").addEventListener ("input", myFunction, false);

function myFunction() {
  if(this.value > 100)
  {
    this.value = 100;
  }
}

To see how it works, I leave the jsfiddle

link

Greetings and successes!

    
answered by 02.05.2018 в 18:49
0

Assuming that you have used DataAnnotations in your model to validate, example [Required] so that the field is not null or [DataType(DataType.Date)] to validate that the field is of type Date, you can also create a Custom Validation, in this case for that the last number is less than 100. Example

in your Models folder creates a new name class LessQue100Attribute with the following code.

public class MenorQue100Attribute : ValidationAttribute
    {

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var numero = int.Parse(value.ToString());
            if (numero > 100)
            {
                return new ValidationResult(ErrorMessage = "El número es mayor que 100");
            }

            return ValidationResult.Success;
        }
    }

and in your model you can already add that dataannotation as when you add [Required] example:

using System.ComponentModel.DataAnnotations;

public class Auto
    {
       [Key, ForeignKey("Conductor")]
        public int ConductorId { get; set; }
        public string Modelo { get; set; }
        [MenorQue100]
        public int Millas { get; set; }
        public virtual Conductor Conductor { get; set; }
    }

This way you have created a CustomValidation which does not allow you to add an Auto with more than 100 miles. I hope it helps you

I have edited my answer because as Gbianchi reminded me, there is already a DataAnnotation for this type of problem [Range] example;

using System.ComponentModel.DataAnnotations;   

public class Auto
    {
       [Key, ForeignKey("Conductor")]
        public int ConductorId { get; set; }
        public string Modelo { get; set; }
        [Range(0,100, ErrorMessage = "Mayor que 100")]
        public int Millas { get; set; }
        public virtual Conductor Conductor { get; set; }
    }

In any case it is good to know the first way if there is a case in which it is necessary to use a validation type which is not found in the DataAnnotations. Greetings

    
answered by 02.05.2018 в 17:54