Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
119
rated 0 times [  121] [ 2]  / answers: 1 / hits: 9927  / 10 Years ago, thu, april 17, 2014, 12:00:00

I am not so good with jquery and hence need your help.



I have a form in which there are two fields origin and destination. So I am using the jquery autocomplete for both the fields.



$('#form1_origin,#form1_destination').autocomplete({
lookup: countriesArray,
minChars: 0,
});


It works great but when I am trying to validate these fields using jquery validation plugin, I get some problems.



Here is the code:-



$('#form_1').validate({
errorClass: airError,
focusInvalid: true,
success: function(element){
$(element).closest('.airError').removeClass('.airError');
},
errorPlacement: function(error,element){
if(element.attr(name) === form1_infant || element.attr(name) === form1_departure){
error.insertAfter('#showErrors');
}else{
error.insertAfter(element);
}
},
rules: {
form1_origin: {
validSelectionOrigin: true,
},
form1_destination:{
validSelectionDestination: true,
compare_origin_dest: true
},
// rules for rest of the fields
},
});


Problem:-



As I am pretty sure that jquery validates the fields on onkeyup. So If I TYPE the value in this field, it works fine but when I select the value from the autocomplete list, It does not validate the field as I can still see the error being displayed. So what can i do to make it work for both ??


More From » jquery

 Answers
7

Quote OP:




As I am pretty sure that jquery validates the fields on onkeyup. So If I TYPE the value in this field, it works fine but when I select the value from the autocomplete list, It does not validate the field as I can still see the error being displayed. So what can i do to make it work for both?




The jQuery Validate plugin evaluates text input elements by several triggers... key-up, blur, and submit button click (all fields at once).



The jQuery Validate plugin also provides you with the .valid() method where you can trigger validation programatically.



I don't think you've shown enough relevant code, but basically, you'll need to trigger .valid() whenever the value of the field changes.



Adjust code as required...



$('input[name=autocompletedField]').on('change', function() {
$(this).valid(); // trigger validation test
});

[#45949] Wednesday, April 16, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lucianom

Total Points: 601
Total Questions: 98
Total Answers: 109

Location: Kenya
Member since Fri, Dec 23, 2022
1 Year ago
lucianom questions
Tue, Feb 22, 22, 00:00, 2 Years ago
Wed, May 5, 21, 00:00, 3 Years ago
Sun, Jan 24, 21, 00:00, 3 Years ago
Sat, Aug 15, 20, 00:00, 4 Years ago
Mon, Jun 22, 20, 00:00, 4 Years ago
;