简体   繁体   English

如何在客户端流星中使用自动格式化中的正则表达式来验证字段的输入值?

[英]How to validate a field's input value using regular expression in autoform in meteor on client side?

i'm new to meteor framework and i'm building a form which needs validation of fields using regular expression in client side. 我是流星框架的新手,我正在构建一个需要在客户端使用正则表达式验证字段的表单。

schema.js schema.js

   PRODUCT_BUILD:{
    type:String,
    label:' PRODUCT_BUILD', 
    defaultValue:"PRODUCT:latest",
    regEx: /^(PRODUCT)((\/(([0-9]+\.)+[0-9]+))|(\:(latest)))/
  },

I have written the message as 我写的消息是

SimpleSchema.messages({
 'regEx PRODUCT_BUILD': "Can have eg: PRODUCT/10.X.X.1234 or PRODUCT:latest",
});

test.html test.html

 {{> afQuickField name='PRODUCT_BUILD'}}

I have entered all the fields which regex needs, but when I give the invalid input it dosen't check the regex and also the error message. 我已经输入了正则表达式需要的所有字段,但是当我输入无效的输入时,它不会同时检查正则表达式和错误消息。
Can anyone tell me what i can do to correct it? 谁能告诉我我可以做些什么来纠正它?

As for as I remember, the way simple schema messages are handled have been changed in the recent versions of simple-schema / autoform. 就我所记得的而言,在最近版本的simple-schema / autoform中,已更改了处理简单模式消息的方式。

Ideally, it used to be something like this (which I'm not sure if it's going to work on your version): 理想情况下,它曾经是这样的(我不确定它是否适用于您的版本):

SimpleSchema.messages({
  "regEx PRODUCT_BUILD": [
    { msg: "Can have eg: PRODUCT/10.X.X.1234 or PRODUCT:latest" }
  ]
});

There is an alternate method using custom field validation as below: 还有一种使用自定义字段验证的替代方法,如下所示:

  PRODUCT_BUILD:{
    type:String,
    label:' PRODUCT_BUILD', 
    defaultValue:"PRODUCT:latest",
    custom: function(){
            if(this.value){
               // do the regex validation using normal JS
              //if validation fails,
               return "regexError"
             }
           }
  },


SimpleSchema.messages({
  "regexError":  "Can have eg: PRODUCT/10.X.X.1234 or PRODUCT:latest" 

});

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

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