I’m using jquery validate v1.7 and jQuery v1.4.2 and I have to use .live on blur and onkeyup events. These events dont seem to work with these versions. Any solution on how to deal with onkeyup and blur events with these constraints would be helpful.
Note:I won’t be able to update these versions to the latest versions available because multiple teams are using the old versions of these files and they dont want to upgrade to the latest.
Jquery validate call:
onkeyup: function(element){
C.options.throwMessages(element);
},
throwMessages: function(el){
if($(el).attr('name') === "email"){ //Validating email field
var selectedField = $(el);
var emailValid = C.options.simple_validate_email(selectedField);
if (C.options.pageName === 'login' || C.options.pageName === 'register'){
$(C.options.currentPage).find(':submit').click(function(){
var selectedField = $(C.options.currentPage).find('input.email');
if(!selectedField.val().length){
selectedField.removeClass('errorIndicator');
}
});
}
if(C.options.pageName === "register"){
selectedField.live('blur', function(){
if($(this).val().length === 0){
$(this).parent().parent().removeClass('aimValid');
}
});
selectedField.live('keydown focusout', function(event) {
if(event.type == 'keydown'){
if(!C.options.simple_validate_email($(this))){
$(this).parent().parent().removeClass('aimValid');
$(C.options.currentPage).find('.emailTip').show().html('Please enter a valid email address.');
}else{
$(this).removeClass('errorIndicator');
$(C.options.currentPage).find('.emailTip').hide().html('');
}
}else if(event.type == 'focusout'){
if(!C.options.simple_validate_email($(this))){
}else{
$(this).removeClass('errorIndicator');
$(C.options.currentPage).find('.emailTip').hide().html('');
}
}
});
}
}else if($(el).attr('name') === "password"){//Validating password field
var selectedPasswordField = $(el);
if(C.options.pageName === "register"){
//password field
selectedPasswordField.live('keydown', function(){
if($(this).val().length === 0){
$(this).parent().parent().removeClass('aimValid');
$(this).removeClass('aimError');
}
});
}
}else if($(el).attr('name') === "username2"){//Validating email field on ForgotPassword screen
var selectedField = $(el);
if (C.options.pageName === 'forgotPassword') {
var isFormValid = false;
$(C.options.currentPage).find(':submit').click(function(){
if((selectedField.val().length === 0) || !selectedField.parent().parent().find('div.aimError').length){
isFormValid = true;
}
selectedField.live('keydown focusout', function(event){
if(event.type === 'keydown'){
$(C.options.currentPage).find('.emailTip').hide().html('');
}else{
$(C.options.currentPage).find('.emailTip').hide().html('');
}
});
if (isFormValid) {
$(C.options.currentPage).find('.emailTip').show().html('Please enter a valid email address.');
}else{
$(C.options.currentPage).find('.emailTip').hide().html('');
}
});
}
}
},
.live()has been deprecated in all versions of jQuery. For 1.4, you can use.delegate()like this:Working demo: http://jsfiddle.net/jfriend00/WxqwS/
For 1.7, you can use
.on()which is pretty much the same as.delegate()except the arguments are in a bit more logical order:Working demo: http://jsfiddle.net/jfriend00/FULfe/