简体   繁体   English

如何验证语义UI中的十进制字段?

[英]How to validate decimal fields in semantic-ui?

In semantic-ui, is it possible to validate decimal numbers? 在语义UI中,可以验证十进制数字吗? For integers one can easily set: 对于整数,可以轻松设置:

$('.ui.form')
  .form({
    fields: {
     field1: {
       rules: [
          {
            type   : 'integer[1..20]',
          }
       ]
     },
  ....

and have the integer validated only if it falls into the specified range. 并且只有在落入指定范围内时才对整数进行验证。 I'd need to do the same for a decimal input (that needs to fall into the range 1.99 - 100.99) but the same approach used for integers does not seem to work. 对于十进制输入,我需要做同样的事情(需要落在1.99-100.99的范围内),但是用于整数的相同方法似乎不起作用。
How can I achieve this? 我该如何实现? Thank you. 谢谢。

I solved this issue like this: 我这样解决了这个问题:

$(document).ready(function() {

  $.fn.form.settings.rules.greaterThan = function (inputValue, validationValue) {
      return inputValue >= validationValue;
  };
  $.fn.form.settings.rules.smallerThan = function (inputValue, validationValue) {
      return inputValue <= validationValue;
  }
  $('.ui.form')
    .form({
      on: 'blur',
      fields: {
        decimal: {
          rules: [
            {
              type   : 'decimal',
              prompt : 'Please enter a decimal number here'
            },
            {
              type: 'greaterThan[1.99]',
              prompt: 'The price should be greater than 1.99',
            },
            {
              type: 'smallerThan[100.99]',
              prompt: 'The price should be less than 100.99',
            },
          ]
        },
      }
    })
  ;
});

In this way, you'll validate upon three rules that combine to get the desired results. 这样,您将验证结合在一起以获得所需结果的三个规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM