0

I use a regular expression to validate a price, but the error message appears before I finish to insert the value.

For example, I what yo insert the value "10.20", and when I get to "10." the message error is presented.

How can I avoid this message to appear until I finish to insert the all value?

Model:

[Required(ErrorMessage = "Preenchimento obrigatório.")]
[Display(Name = "Valor em €")]
[RegularExpression(@"^(?!0\d|$)\d*(\.\d{1,2})?$", ErrorMessage = "Valor inválido.")]
public decimal Valor { get; set; }

HTML:

<input data-val="true" data-val-number="The field Peso Líquido em kg must be a number." data-val-regex="Valor inválido." data-val-regex-pattern="^(?!0\d|$)\d*(\.\d{1,3})?$" data-val-required="Preenchimento obrigatório." id="PesoLiquido" name="PesoLiquido" placeholder="Ex. 10.200" type="text" value="" class="input-validation-error">

The validation is made automatically by the library jquery.validate.unobtrusive.js

Thanks

Patrick
  • 2,995
  • 14
  • 64
  • 125

1 Answers1

0

Assuming this is a client-side (i.e. web browser) script, using something like onChange() or onBlur(), it should be easy enough to validate on the given event and detect the boolean outcome. However if you're using onChange() the validation will run as soon as you start typing, whereas if you were to use onBlur() the validation would only run after focus moves away from that input field. If you really want to use onChange() then you will need to code extra checks to periodically poll the contents (based on key-up detection perhaps) in order to allow time to type the full value. I'm not familiar with the syntax you have used so if you're coding within a framework rather than rolling your own then I can't comment any further. It's pretty standard stuff and usually with bugs like that it's something very simple and [eventually] obvious! Good luck.

  • Hi thanks, but the problem here is that the library jquery.validate.unobtrusive.js controls it automatically, so I need to avoid this auto validation until submit – Patrick Feb 27 '14 at 11:58
  • Ah sorry missed the line where you mentioned jquery. Haven't used it before, my apologies. – user3360243 Feb 27 '14 at 12:03