pandora/static/js/autovalidate.js

115 lines
3.8 KiB
JavaScript
Raw Normal View History

2011-07-29 18:37:11 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=javascript
2011-10-22 14:50:53 +00:00
2011-11-05 17:04:10 +00:00
'use strict';
2011-05-25 19:42:45 +00:00
pandora.autovalidateCode = function(value, blur, callback) {
2011-10-22 15:31:12 +00:00
value = value.toUpperCase().split('').map(function(v) {
2011-10-22 14:50:53 +00:00
return /[A-Z]/.test(v) ? v : null;
2012-05-24 09:56:52 +00:00
}).join('').slice(0, 16);
2011-10-22 14:50:53 +00:00
callback({valid: value.length == 16, value: value});
2011-05-25 19:42:45 +00:00
};
pandora.autovalidateEmail = function(value, blur, callback) {
value = value.toLowerCase().split('').map(function(v, i) {
2011-09-29 17:25:04 +00:00
return /[0-9a-z\.\+\-_@]/.test(v) ? v : null;
2012-05-24 09:56:52 +00:00
}).join('').slice(0, 255);
2011-10-22 14:50:53 +00:00
callback({valid: Ox.isValidEmail(value), value: value});
2011-05-25 19:42:45 +00:00
};
pandora.autovalidateListname = function(value, blur, callback) {
2011-10-22 14:50:53 +00:00
// A valid listname consists of 1 to 255 unicode characters,
// without leading, trailing or consecutive spaces
2011-05-25 19:42:45 +00:00
var length = value.length;
2011-10-22 14:50:53 +00:00
value = value.toLowerCase().split('').map(function(v, i) {
return /\s/.test(v) && (i == 0 || (i == length - 1 && blur)) ? null : v;
2011-05-25 19:42:45 +00:00
}).join('');
2012-05-24 09:56:52 +00:00
value = value.replace(/\s+/g, ' ').slice(0, 255);
2011-10-22 14:50:53 +00:00
callback({valid: !!value.length, value: value});
2011-05-25 19:42:45 +00:00
};
pandora.autovalidateUsername = function(value, blur, callback) {
2011-10-22 14:50:53 +00:00
// A valid username consists of 1 to 255 unicode characters,
// without leading, trailing or consecutive spaces
2011-05-25 19:42:45 +00:00
var length = value.length;
value = value.toLowerCase().split('').map(function(v, i) {
2011-10-22 14:50:53 +00:00
return /\s/.test(v) && (i == 0 || (i == length - 1 && blur)) ? null : v;
2011-05-25 19:42:45 +00:00
}).join('');
2012-05-24 09:56:52 +00:00
value = value.replace(/\s+/g, ' ').slice(0, 255);
2011-10-22 14:50:53 +00:00
callback({valid: !!value.length, value: value});
};
pandora.validateCode = function(value, callback) {
callback({
message: '',
// message: 'Missing code',
valid: value.length > 0
});
};
2011-10-22 14:50:53 +00:00
pandora.validateNewEmail = function(value, callback) {
value == pandora.user.email ? callback({
message: '',
valid: true,
value: value
2011-10-22 14:50:53 +00:00
}) : Ox.isValidEmail(value) ? pandora.api.findUser({
key: 'email',
value: value,
operator: '=='
}, function(result) {
callback({
message: !!result.data.users.length ? Ox._('E-Mail Address already exists') : '',
valid: !result.data.users.length,
value: value
2011-10-22 14:50:53 +00:00
});
}) : callback({
2013-05-09 10:13:58 +00:00
message: value.length ? Ox._('Invalid e-mail address') : '',
// message: (!value.length ? 'Missing' : 'Invalid') + ' e-mail address',
valid: false,
value: value
2011-10-22 14:50:53 +00:00
});
};
pandora.validateNewPassword = function(value, callback) {
callback({
message: '',
// message: 'Missing password',
valid: value.length > 0,
value: value
});
};
pandora.validatePassword = function(value, callback) {
callback({
message: '',
// message: 'Missing password',
valid: value.length > 0,
value: value
2011-10-07 17:56:39 +00:00
});
2011-05-25 19:42:45 +00:00
};
pandora.validateUser = function(key, existing) {
existing = existing || false;
var string = key == 'username' ? 'username' : 'e-mail address';
return function(value, callback) {
var valid = key == 'username' ? !!value.length : Ox.isValidEmail(value);
valid ? pandora.api.findUser({
key: key,
value: value,
2011-10-22 14:50:53 +00:00
operator: '=='
2011-05-25 19:42:45 +00:00
}, function(result) {
var valid = (existing == !!result.data.users.length) || pandora.site.site.dontValidateUser;
2011-05-25 19:42:45 +00:00
callback({
message: existing ?
2013-05-09 10:13:58 +00:00
Ox._('Unknown ' + string) :
Ox._(string[0].toUpperCase() + string.slice(1) + ' already exists'),
2011-05-25 19:42:45 +00:00
valid: valid
});
}) : callback({
2013-05-09 10:13:58 +00:00
message: value.length ? Ox._('Invalid ' + string) : '',
// message: (!value.length ? 'Missing' : 'Invalid') + ' ' + string,
2011-05-25 19:42:45 +00:00
valid: false
});
};
};