【问题标题】:Using regular expression in Ext Js VType在 Ext Js VType 中使用正则表达式
【发布时间】:2011-02-11 07:17:01
【问题描述】:

我有一个用于检查数字并允许“-”(连字符)文本字段的正则表达式。

var regex = /^\d+-\d{1,2}$/;  //Checks "digits-digit(s,1 or 2)"

这适用于常规 HTML 文本字段。但是如果我想要一个 Ext Js TextField 我必须执行以下代码

Ext js TextField 并称为 VType

var <portlet:namespace/>issueNoField = new Ext.form.TextField({
     fieldLabel: 'Issue No',
     width: 120,
     valueField:'IssNo',
     vtype: 'hyphen'
 });

Ext.apply(Ext.form.VTypes, {
                hyphenText : "Only numbers and hyphen.",
                hyphenMask:/[0-9-]/,
                hyphenRe: /^\d+-\d{1,2}$/,  //This is the check
                hyphen:function(x){return this.hyphenRe.test(x);}   //Am i missing a numericHyMask: here ??
            });     

      Is hyphenRe: /^\d+-\d{1,2}$/, is correct or
      is hyphenRe: /^\d+-[\d{1,2}]$/, is correct as I want 1 or 2 digits after '-'

请帮助我更改我的 VType 以使其正常工作并进行正则表达式检查。

【问题讨论】:

    标签: javascript regex validation extjs


    【解决方案1】:

    对我来说很好用。验证本身是否不起作用(当您在其中键入无效内容时,该字段没有下划线)或者您是否缺少错误工具提示?在后一种情况下,在您的代码中添加某处:

    Ext.QuickTips.init();
    

    否则工具提示不会出现。

    【讨论】:

    • 我在上面编辑了我的脚本,请建议。我在脚本上方使用 QuickTips。
    • 我测试了你的代码,它工作得很好。除了我遗漏的&lt;portlet:namespace/&gt;。该字段很好地验证了您的正则表达式。但我看到现在你添加了一个不正确的正则表达式/^\d+-[\d{1,2}]$/。您在编写正则表达式时遇到问题吗?在这种情况下,请更详细地解释您真正希望该正则表达式实现什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    • 2017-01-18
    • 2017-11-13
    • 1970-01-01
    相关资源
    最近更新 更多