Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

167
Vistas
How to add multiple messages for validation based on the condition knockout js?

I have two error messages but only one is shown based on vendor value. The initial value for vendor as application starts is null. After that, based on the user input the vendor value is changed. However, the validation error message says "Input is required" regardless of vendor value - even the condition is met. It seems to me that the value for self.agreement is not updated.

I have looked at here and here , and applied the techniques but none of them worked for me.

index.js

 self.agreement = (vendor == "BAS")? ko.observable().extend({
      required:{message:"Should be 9 digit "},
      pattern:{
        params:/^\d{9}$/
      }
    }):ko.observable().extend({ required:{message:"Input is required", onlyIf:function(){return !self.sample()}} });

index.cshtml

<input data-bind="init, value: agreement" class="form-control" type="text" value="@(Model.Ani)" />
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I think you're re-scoping the observable.

ko.observable().extend({}):ko.observable().extend({})

Try it like this instead

ko.observable().extend({}).extend({}).extend({})......

Here's a sample.

Price: local.Price.extend({ required: true }).extend({ number: true }).extend({ pattern: '(\$\s*)?(([1-9]\d*)(\.\d+)?|0?\.\d*[1-9]\d*)' })
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda