最好使用简单的验证器函数进行检查。您可以使用以下代码并根据需要进行更改(删除错误图标,更改样式并添加“密码强度:字符串”标题):
Ext.define('PasswordField', {
extend: 'Ext.form.field.Text',
alias: 'widget.passwordfield',
//inputType: 'password',
msgTarget: 'under',
validators: [{
errorMessage: "Password should contain at least 6 character;",
fn: (value) => {
return value.length >= 6
}
}, {
errorMessage: "Password should contain at least one number;",
fn: (value) => {
return /\d/.test(value)
}
}, {
errorMessage: "Password should contain at least one lowercase and one uppercase letter;",
fn: (value) => {
return /[a-z]/.test(value) && /[A-Z]/.test(value);
}
}, {
errorMessage: "Password should contain at least one special character;",
fn: (value) => {
return /[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(value);
}
}],
initComponent: function () {
this.callParent();
},
onRender: function() {
this.callParent();
this.validate();
},
validator: function(val) {
const errorMessages = [];
this.validators.map( (validator, index) => {
const icon = validator.fn(val) ? '<i class="fa fa-check-circle-o" style="color: green; width: 20px;"></i>': '<i style="width: 20px;"> </i>';
errorMessages.push(`<li>${icon}${validator.errorMessage}</li>`);
});
return errorMessages.join('');
}
});
Ext.application({
name: 'Fiddle',
launch: function () {
Ext.create('Ext.form.Panel', {
title: "Sample Form Panel",
renderTo: Ext.getBody(),
height: 400,
bodyPadding: 5,
items: [{
xtype: 'passwordfield',
name: 'password',
msgTarget: 'under',
labelAlign: 'top',
fieldLabel: "Password"
}]
})
}
});
附言
验证更好地转移到单独的类。