简体   繁体   English

正则表达式无法正常工作

[英]Regex doesn't work properly

I am trying to implement a regular expression to allow only one or two digits after a hyphen '-' and it doesn't work properly. 我正在尝试实现一个正则表达式,以允许在连字符'-'后仅允许一两位数字,并且它不能正常工作。 It allows as many digits as user types after '-' 它允许在'-'之后输入与用户类型一样多的数字

Please suggest my ExtJS 请建议我的ExtJS

Ext.apply(Ext.form.VTypes, {
  hyphenText: "Number and hyphen",
  hyphenMask: /[\d\-]/,
  hyphenRe: /^\d+-\d{1,2}$/,
  hyphen: function(v){
    return Ext.form.VTypes.hyphenRe.test(v);
  }
});

    //Input Field for Issue no
 var <portlet:namespace/>issueNoField = new Ext.form.TextField({
 fieldLabel: 'Issue No',
   width: 120,
   valueField:'IssNo',
   vtype: 'hyphen'

 });

This works only to the limit that it allows digits and - . 这仅在允许数字和-的范围内起作用。 But it also has to allow only 1 to 2 digits after - at most. 但是它也必须只允许12位数字-最多。

Is something wrong in my regex? 正则表达式有问题吗? hyphenRe: /^\\d+-\\d{1,2}$/,

It could be that it expects the input to match exactly with the Regex pattern you have specified. 可能是因为它期望输入与您指定的Regex模式完全匹配。 This site has a zip code example that also limits the numbers after the hyphen. 该站点的邮政编码示例也限制了连字符后的数字。

It uses an Else statement to let form function accept one pattern or another pattern option. 它使用Else语句让表单函数接受一个模式或另一种模式选项。

The Regex used for the 5-4 format is: "^\\d{5}-\\d{4}$" 用于5-4格式的正则表达式为:“ ^ \\ d {5}-\\ d {4} $”

Hope this helps a bit. 希望这个对你有帮助。

Ext.form.VTypes.hyphenRe.test(v) should do the check and not allow the user to enter two - at first place. Ext.form.VTypes.hyphenRe.test(v)应该进行检查,并且不允许用户输入两个-首先。

What you suggest is to check if there is any - in field which is totally a different ball game. 您的建议是检查是否有任何-完全不同的球类游戏。

The hyphenRe is somehow not being called. hyphenRe不被以某种方式调用。

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

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