Files

121 lines
4.0 KiB
JavaScript

Friends.FieldValidator = new function() {
this.prefix = 'FieldValidator-';
this.createFromTag = function(element, params) {
new Friends.FieldValidator[params.type](element, params);
}
this.getNotes = function(element) {
var notes = {__visible__: null};
var tags = y5.Dom.getDescendants(element, '*', new RegExp( this.prefix + 'note-\\w+', 'ig'));
for(var i = 0, l = tags.length; i < l; i++) {
var name = y5.Components.getName(tags[i].className, this.prefix + 'note-')
notes[name] = tags[i];
if (notes[name].style.display != 'none') {
notes.__visible__ = notes[name];
}
}
return notes;
}
this.showNote = function(notes, note) {
if (notes.__visible__) {
notes.__visible__.style.display = 'none';
}
if (notes[note]) {
notes[note].style.display = 'block';
notes.__visible__ = notes[note];
}else{
notes.__visible__ = null;
}
};
this.onlyNumbers = function(string) {
return /^\d+$/.test(string);
};
/**
* input states
*/
this.ERROR = "error";
this.SUCCESS = "success";
this.EMPTY = "empty";
this.PROVED = "proved";
this.NOT_PROVED = "notProved";
};
Friends.FieldValidator.ICQ = function(element, params) {
var _this = this;
this.field = y5.Dom.getDescendant(element, '*', this.prefix + 'field');
this.notes = this.getNotes(element);
function check() {
_this.check();
}
y5.Events.create('keyup', check, element, true);
this.check = function() {
var result = this.SUCCESS;
if (this.field.value.length && !this.onlyNumbers(this.field.value)) {
result = this.ERROR;
}
y5.Events.notify('y5:validationState', this.field, true, {state: result, element: this.field});
this.showNote(this.notes, result);
};
};
Friends.FieldValidator.ICQ.prototype = Friends.FieldValidator;
Friends.FieldValidator.Email = function(element, params){
var _this = this;
this.field = y5.Dom.getDescendant(element, '*', this.prefix + 'field');
this.notes = this.getNotes(element);
function check(e) {
if (typeof(e.value) != y5.Vars.UNDEF && e.value === null){
return;
}
_this.check(true);
}
if (params.eventType) {
var elements = document.getElementsByTagName(params.eventTargetTagName);
if (elements.length > 0) {
var obj = elements[params.eventTargetName];
new y5.AEventListener(params.eventType,
function(e) {
if (_this.check() == _this.ERROR) {
e.preventDefault();
}
}, obj, true);
}
}
else {
y5.Events.create('change', check, this.field, true);
y5.Events.observe('y5:change', check, this.field, true);
}
this.email = function(string){
return /^[\w-]+([\.+][\w-]+)*@([\w-]+\.)+[a-z]{2,7}$/i.test(string);
};
this.check = function(init) {
var result = this.NOT_PROVED;
var email = this.field.value;
var placeholder = this.field.getAttribute('placeholder');
if ((email == '' || placeholder && email == placeholder) && init){
result = this.EMPTY;
}else if (new RegExp('\\|' + email + '\\|').test(this.validated)){
result = this.PROVED;
}else if(!this.email(email)) {
result = this.ERROR;
}
y5.Events.notify('y5:validationState', this.field, true, {state:result,element:this.field});
this.showNote(this.notes, result);
return result;
};
this.getValidatedEmails = function(string) {
return string.replace(/<[^>]*>/ig, '|').replace(/\|\|/ig, '|');
};
this.validated = params.data ? this.getValidatedEmails(params.data) : '';
this.check(true);
}
Friends.FieldValidator.Email.prototype = Friends.FieldValidator;
y5.require(['Events'], function() {y5.loaded('{Friends}.FieldValidator')})