Validating specific input fields and triggering on click event
-
Hello codepeople!
I have buttons (add room1, add room2..) and they have many on click events ( .show().hide() etc.) When user clicks button they see new room’s input fields. Then, they are able to add another room too. I want to do that if first room information are empty and user want to add new room (click add room button);
1) Stop all defined on click events! (do nothing)
2) First fill first room information (force validating)
3) If validation ok ,now when button clicked trigger all on click events.I tried to do it my code. I will be glad if you show me my logic errors.
Thanks in advance.<script> jQuery(document).on( 'click', '[id*="fieldname'+'155_"]', function() validateMyForm { //when user click this button (Add New Room Button) var result = 'TRUE'; //string value + I am not sure about that variable is necessary var value = jQuery('[id*="fieldname'+'199_"]').val(); // call the value and assign it variable if (value != 0) { //if all three input fields have a value (fieldname'+'199 = fieldname1*fieldname2*fieldname3) jQuery('.room2').show(); jQuery('.x2').hide(); jQuery('.x2').addClass('deneme3'); jQuery('.x3').addClass('deneme3'); jQuery('.x5').hide(); jQuery('.x8').show(); jQuery( '.approve11 label').html( '2 Rooms Added' ); } else { var result = 'FALSE'; // i am not sure - return false; - assign false value to function automatically so i added second result variable too return false; //i mean don't trigger this function (break it) if input fields are empty (value == 0) (do nothing, don't apply above jquery events) } if (validateMyForm() == false || result == 'FALSE' ) { //if function breaked and returned false, validate the these three fields first jQuery('[id*="fieldname'+'6_"],[id*="fieldname'+'7_"],[id*="fieldname'+'13_"]').addClass('required'); // make them required first jQuery(this).closest('[id*="fieldname'+'6_"],[id*="fieldname'+'7_"],[id*="fieldname'+'13_"]').valid(); // second validate them } if jQuery(this).closest('[id*="fieldname'+'6_"],[id*="fieldname'+'7_"],[id*="fieldname'+'13_"]').valid() { // if validation is ok trigger click events jQuery('.oda2').show(); jQuery('.x2').hide(); jQuery('.x2').addClass('deneme3'); jQuery('.x3').addClass('deneme3'); jQuery('.x5').hide(); jQuery('.x8').show(); jQuery( '.onaylama11 label').html( '2 Oda Eklendi' ); } else { //if validation is not ok return validateMyForm(); //trigger the function } }); </script>
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Validating specific input fields and triggering on click event’ is closed to new replies.