PDA

View Full Version : multiple email addresses validation



ext_fan
6 Apr 2008, 5:20 PM
Hi - I have a textfield to capture multiple emails seperated by delimiter;
how to do client side validations for multiple addresses

by default ext js provides type:'email' for a field but this works only for one email

dj
6 Apr 2008, 5:32 PM
Have a look at src/widgets/form/VTypes.js
Add your own 'multipleemail' vtype to the Ext.form.VTypes singelton with an appropiate regular expression.

Zyclops
16 Apr 2008, 7:58 PM
Here is my regex I use for multi-email validations, lets you separate with either a , or ;

^([-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*[email protected](?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?|,|\;)+$

Zyclops
17 Apr 2008, 4:14 PM
I ran into performance issues with using my previous regex alone, so I slightly modified to split the string and regex the parts individually. I think has more to do with spidermonkeys regex engine.

I've just overridden the entire vtypes class, please tell me if that's not the appropriate way to override the original class :)



/**
* @class Ext.form.VTypes
* Overridable validation definitions. The validations provided are basic and intended to be easily customizable and extended.
* @singleton
*/
Ext.form.VTypes = function(){
// closure these in so they are only created once.
var alpha = /^[a-zA-Z_]+$/;
var alphanum = /^[a-zA-Z0-9_]+$/;
var email = /^([\w]+)(.[\w]+)*@([\w-]+\.){1,5}([A-Za-z]){2,4}$/;
var url = /(((https?)|(ftp)):\/\/([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\\/[email protected]&#;`~=%!]*)(\.\w{2,})?)*\/?)/i;

// All these messages and functions are configurable
return {
/**
* The function used to validated multiple email addresses on a single line
* @param {String} value The email addresses - separated by a comma or semi-colon
*/
'multiemail' : function(v) {
var array = v.split(',');
var valid = true;
Ext.each(array, function(value) {
if (!email.test(value)) {
valid = false;
};
});
return valid;
},
/**
* The error text to display when the multi email validation function returns false
* @type String
*/
'multiemailText' : 'This field should be an e-mail address, or a list of email addresses separated by semi-colons(:) or commas(,) in the format "[email protected];[email protected]"',
/**
* The keystroke filter mask to be applied on multi email input
* @type RegExp
*/
'multiemailMask' : /[a-z0-9_\.\[email protected]\,]/i,

/**
* The function used to validate email addresses
* @param {String} value The email address
*/
'email' : function(v){
return email.test(v);
},
/**
* The error text to display when the email validation function returns false
* @type String
*/
'emailText' : 'This field should be an e-mail address in the format "[email protected]"',
/**
* The keystroke filter mask to be applied on email input
* @type RegExp
*/
'emailMask' : /[a-z0-9_\.\[email protected]]/i,

/**
* The function used to validate URLs
* @param {String} value The URL
*/
'url' : function(v){
return url.test(v);
},
/**
* The error text to display when the url validation function returns false
* @type String
*/
'urlText' : 'This field should be a URL in the format "http:/'+'/www.domain.com"',

/**
* The function used to validate alpha values
* @param {String} value The value
*/
'alpha' : function(v){
return alpha.test(v);
},
/**
* The error text to display when the alpha validation function returns false
* @type String
*/
'alphaText' : 'This field should only contain letters and _',
/**
* The keystroke filter mask to be applied on alpha input
* @type RegExp
*/
'alphaMask' : /[a-z_]/i,

/**
* The function used to validate alphanumeric values
* @param {String} value The value
*/
'alphanum' : function(v){
return alphanum.test(v);
},
/**
* The error text to display when the alphanumeric validation function returns false
* @type String
*/
'alphanumText' : 'This field should only contain letters, numbers and _',
/**
* The keystroke filter mask to be applied on alphanumeric input
* @type RegExp
*/
'alphanumMask' : /[a-z0-9_]/i
};
}();

dj
17 Apr 2008, 4:33 PM
This should do it without overriding the whole thing.
Ext.formVTypes is a singleton with private variables and email is one of them. In the override (without copying the whole thing) we cannot access it. But then, Ext.form.VTypes.email() does what we want, so we can use that instead.


Ext.apply(Ext.form.VTypes, {
/**
* The function used to validated multiple email addresses on a single line
* @param {String} value The email addresses - separated by a comma or semi-colon
*/
'multiemail' : function(v) {
var array = v.split(',');
var valid = true;
Ext.each(array, function(value) {
if (!this.email(value)) {
valid = false;
return false;
};
}, this);
return valid;
},
/**
* The error text to display when the multi email validation function returns false
* @type String
*/
'multiemailText' : 'This field should be an e-mail address, or a list of email addresses separated by commas(,) in the format "[email protected],[email protected]"',
/**
* The keystroke filter mask to be applied on multi email input
* @type RegExp
*/
'multiemailMask' : /[a-z0-9_\.\[email protected]\,]/i
});

Zyclops
17 Apr 2008, 10:41 PM
@dj - thankyou kindly :)