Jump to content
  • 0

Можно ли перевести Mootools плагин в jQuery?


Bebeka
 Share

Question

Привет!

В чем отличия этих модулей? У меня код всего на 1 страницу... И он написан на mootools... А все остальные плагины на jQuery .. От чего из-за одного плагина на Мутулс приходится подключать сам модуль мутулс который весит под 100 кб... А хочется сайт как-то оптимизовать, хоть он и для веб...


var JFormValidator = new Class({
initialize: function()
{
// Initialize variables
this.handlers = Object();
this.custom = Object();

// Default handlers
this.setHandler('name',
function (value) {
regex=/^[ а-яёА-ЯЁa-zA-Z0-9_-]+$/g;
return regex.test(value);
}
);

// Default handlers
this.setHandler('username',
function (value) {
regex=/^[a-zA-Z0-9_-]+$/g;
return regex.test(value);
}
);

this.setHandler('password',
function (value) {
regex=/^\S[\S ]{2,98}\S$/;
return regex.test(value);
}
);

this.setHandler('numeric',
function (value) {
regex=/^(\d|-)?(\d|,)*\.?\d*$/;
return regex.test(value);
}
);

this.setHandler('email',
function (value) {
regex=/^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
return regex.test(value);
}
);

// Attach to forms with class 'form-validate'
var forms = $$('form.form-validate');
forms.each(function(form){ this.attachToForm(form); }, this);
},

setHandler: function(name, fn, en)
{
en = (en == '') ? true : en;
this.handlers[name] = { enabled: en, exec: fn };
},

attachToForm: function(form)
{
// Iterate through the form object and attach the validate method to all input fields.
$A(form.elements).each(function(el){
el = $(el);
if ((el.getTag() == 'input' || el.getTag() == 'button') && el.getProperty('type') == 'submit') {
if (el.hasClass('validate')) {
el.onclick = function(){return document.formvalidator.isValid(this.form);};
}
} else {
el.addEvent('blur', function(){return document.formvalidator.validate(this);});
}
});
},

validate: function(el)
{
// If the field is required make sure it has a value
if ($(el).hasClass('required')) {
if (!($(el).getValue())) {
this.handleResponse(false, el);
return false;
}
}

// Only validate the field if the validate class is set
var handler = (el.className && el.className.search(/validate-([a-zA-Z0-9\_\-]+)/) != -1) ? el.className.match(/validate-([a-zA-Z0-9\_\-]+)/)[1] : "";
if (handler == '') {
this.handleResponse(true, el);
return true;
}

// Check the additional validation types
if ((handler) && (handler != 'none') && (this.handlers[handler]) && $(el).getValue()) {
// Execute the validation handler and return result
if (this.handlers[handler].exec($(el).getValue()) != true) {
this.handleResponse(false, el);
return false;
}
}

// Return validation state
this.handleResponse(true, el);
return true;
},

isValid: function(form)
{
var valid = true;

// Validate form fields
for (var i=0;i < form.elements.length; i++) {
if (this.validate(form.elements[i]) == false) {
valid = false;
}
}

// Run custom form validators if present
$A(this.custom).each(function(validator){
if (validator.exec() != true) {
valid = false;
}
});

return valid;
},

handleResponse: function(state, el)
{
// Find the label object for the given field if it exists
if (!(el.labelref)) {
var labels = $$('label');
labels.each(function(label){
if (label.getProperty('for') == el.getProperty('id')) {
el.labelref = label;
}
});
}

// Set the element and its label (if exists) invalid state
if (state == false) {
el.addClass('invalid');
if (el.labelref) {
$(el.labelref).addClass('invalid');
}
} else {
el.removeClass('invalid');
if (el.labelref) {
$(el.labelref).removeClass('invalid');
}
}
}
});

document.formvalidator = null;
Window.onDomReady(function(){
document.formvalidator = new JFormValidator();
});

Вот собственно и код...

Кстати, если кто СМОЖЕТ могу даже и заплатить на конвертацию с Мутулс на jQuery.... Просто ОЧЕНЬ надо.... Сам только изучаю jquery, поэтому не силен в этом...

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. See more about our Guidelines and Privacy Policy