简体   繁体   English

在 extjs 中扩展电子邮件验证

[英]extending email validation in extjs

I am having the below user registration form built in 6.0.1.250 version of ExtJs.我在 6.0.1.250 版本的 ExtJs 中内置了以下用户注册表单。 I have an email field which accepts .co , .com till the four characters.我有一个电子邮件字段,它接受.co.com直到四个字符。 I need to handle the recent tlds and want to override the email validation logic.我需要处理最近的tlds并希望覆盖电子邮件验证逻辑。 I tried with the validator and applying the regex but the regexText is not supporting internationalisation.我尝试使用validator并应用正则表达式,但regexText不支持国际化。 How to extend the email validation.如何扩展email验证。 I am using 6.0 version of extjs framework.我正在使用 6.0 版本的 extjs 框架。 Below is the code snippet for UserForm下面是用户窗体的代码片段

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'
  }]
});

You could override the email validation vtype like so:您可以像这样覆盖电子邮件验证 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);
    }
});

I took the default email regex and changed so that it allows from 2 to 10 characters for the domain name.我采用了默认的电子邮件正则表达式并进行了更改,以便它允许域名包含 2 到 10 个字符。

Edit编辑

If you use Sencha CMD, there is a special overrides folder where you should put your overrides.如果您使用 Sencha CMD,则有一个特殊的overrides文件夹,您应该在其中放置您的覆盖。 So you can create a file called Vtypes.js , place the code there, and after a sencha app refresh you will be fine.因此,您可以创建一个名为Vtypes.js的文件,将代码放在那里,然后在Vtypes.js sencha app refresh后就可以了。 If you don't - the override should be basically executed before you use the actual vtype.如果不这样做 - 应该在使用实际 vtype 之前基本上执行覆盖。 You might have a method called applyOverrides in your Application.js , and then call it first thing from your launch method.您的Application.js可能有一个名为applyOverrides的方法,然后首先从您的启动方法中调用它。

We can solve above using this simple approach:我们可以使用这种简单的方法解决上述问题:

{
   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