Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

673
Views
¿Cómo poner la expresión regular para validar el número entre 1.0 y 4.5 (rango entre valores decimales)?

Necesito una expresión para validar números entre 1,0 y 4,5 pero no funciona precisamente para eso.

Expresión que estoy usando: /^[1-4]{0,1}(?:[.]\d{1,2})?$/

El requisito es aceptar solo un valor entre 1,0 y 4,5

pero

 buildInserForm(): void { this.insertLeavesForm = this.fb.group({ **your text** hours: [ { value: 4.5, disabled: true }, [ Validators.required, Validators.pattern(**/^[1-4]{0,1}(?:[.]\d{1,2})?$/**), ], ], }); }

Actualmente restringiendo los números de 1.0 a 4.0, pero el problema viene en puntos decimales, muestra un error si ingresa cualquier número entre 6 y 9 en lugares decimales, como 1.7, 2.8, 3.9.

los criterios de aceptación son de 1,0 a 4,5.

Imagen que muestra dónde se está ingresando mal el valor

Esta imagen muestra que el valor se ingresa con múltiples decimales, lo cual es incorrecto,
Solo se requiere un valor de lugar decimal único.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Las expresiones regulares son bastante malas para verificar rangos de números. ¿Debería considerar números no decimales? ¿Qué pasa si hay más de un punto decimal? ¿Qué pasa si alguna vez desea aumentar/disminuir el rango? Aquí hay más sobre el tema:Expresión regular Rango con decimal 0.1 - 7.0

Sugeriría validadores mínimos min/max simples. Además, esto también le permitiría controlar si el valor del usuario está por debajo o por encima de los criterios, lo que le permite mostrar mensajes de error personalizados de manera adecuada, por ejemplo. Mientras que regex simplemente se evaluará como verdadero/falso.

 [ Validators.required, Validators.min(1), Validators.max(4.5), Validators.maxLength(3) ]
about 4 years ago · Juan Pablo Isaza Report

0

Puede usar el siguiente patrón de expresiones regulares:

 ^(?:[1-3](?:\.\d{1,2})?|4(?:\.(?:[0-4][0-9]|50?)?))$

Manifestación

Este patrón de expresiones regulares dice que coincida:

 ^ start of the input (?: [1-3] match 1-3 (?:\.\d{1,2})? followed by optional decimal and 1 or 2 digits | OR 4 match 4 (?: \. decimal point (?:[0-4][0-9]|50?)? match 4.00 to 4.49 or 4.5 or 4.50 ) ) $ end of the input
about 4 years ago · Juan Pablo Isaza Report

0

Esta es una expresión regular que creé. patrón(/^[1-3]{0,1}(?:[.][0-9]{0,1})?[4]{0,1}(?:[.][0-5 ]{0,1})?$/)

Explicación

 /^[1-3]{0,1}(?:[.][0-9]{0,1})?[4]{0,1}(?:[.][0-5]{0,1})?$/ /^ start of the input [1-3] First input to be taken between {0,1} This shows how many times it can be repeated ( Parenthesis shows next entered digit is optional ? Question mark is for using digit zero or once :[.] This is for what the next Character would be eg ".", "@" [0-9] input to be taken between. {0,1} This shows how many times it can be repeated. ) Option part closes ? Question mark is for using digit zero or once. [4] This shows what can be other input that can be taken {0,1} How many times it can be use , SO in this case 0 or 1 times (?:[.] There after again same option part with decimal point decimal value of 4and its limitation for 0 to 5 [0-5] This is to set limitation 0-5 as per our requirement {0,1} Its existence 0 or 1 times ) Close of optional part. ? Thereafter showing the existence of an optional part once or twice. $/ Shows to match expression after it.
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!