簡體   English   中英

在 extjs 中擴展電子郵件驗證

[英]extending email validation in extjs

我在 6.0.1.250 版本的 ExtJs 中內置了以下用戶注冊表單。 我有一個電子郵件字段,它接受.co.com直到四個字符。 我需要處理最近的tlds並希望覆蓋電子郵件驗證邏輯。 我嘗試使用validator並應用正則表達式,但regexText不支持國際化。 如何擴展email驗證。 我正在使用 6.0 版本的 extjs 框架。 下面是用戶窗體的代碼片段

Ext.define('myApp.view.users.NewUserForm', {
extend: 'Ext.panel.Panel',
xtype: 'newuser',
title: '<span class="red-label">*</span>'+ l10n('new-user'),
items: [{
    xtype: 'textfield',
    fieldLabel: '<span class="red-label">*</span>' + l10n('name'),
    name: 'username',
    maxLength: 80,
    allowBlank: false
}, {
    xtype: 'textfield',
    inputType: 'password',
    fieldLabel: '<span class="red-label">*</span>' + l10n('password'),
    name: 'password1',
    vtype: 'password',
    initialPassField: 'password2',
    allowBlank: false,
    maskRe: /[^ ]/
}, {
    xtype: 'textfield',
    fieldLabel: '<span class="red-label">*</span>' + l10n('confirm-password'),
    vtype: 'password',
    inputType: 'password',
    name: 'password2',
    initialPassField: 'password1',
    allowBlank: false,
    maskRe: /[^ ]/
}, {
    xtype: 'textfield',
    fieldLabel: '<span class="red-label">*</span>' + l10n('e-mail-address'),
    name: 'emailAddress',
    vtype:'email',
    allowBlank: false
}, {
    xtype: 'textarea',
    fieldLabel: l10n('description'),
    name: 'userDescription'
  }]
});

您可以像這樣覆蓋電子郵件驗證 vtype:

Ext.define(null, {
    override: 'Ext.form.field.VTypes',
    email: function (value) {
        return /^(")?(?:[^\."\s])(?:(?:[\.])?(?:[\w\-!#$%&'*+/=?^_`{|}~]))*\1@(\w[\-\w]*\.){1,5}([A-Za-z]){2,10}$/.test(value);
    }
});

我采用了默認的電子郵件正則表達式並進行了更改,以便它允許域名包含 2 到 10 個字符。

編輯

如果您使用 Sencha CMD,則有一個特殊的overrides文件夾,您應該在其中放置您的覆蓋。 因此,您可以創建一個名為Vtypes.js的文件,將代碼放在那里,然后在Vtypes.js sencha app refresh后就可以了。 如果不這樣做 - 應該在使用實際 vtype 之前基本上執行覆蓋。 您的Application.js可能有一個名為applyOverrides的方法,然后首先從您的啟動方法中調用它。

我們可以使用這種簡單的方法解決上述問題:

{
   xtype: 'textfield',
   fieldLabel: '<span class="red-label">*</span>' + l10n('e-mail-address'),
   name: 'emailAddress',
   regex : /^(")?(?:[^\."])(?:(?:[\.])?(?:[\w\-!#$%&'*+\/=?\^_`{|}~]))*\1@(\w[\-\w]*\.){1,5}([A-Za-z]){2,6}$/,
   allowBlank: false
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM