javascript - Jquery complete form validation issue

474

I am working on a php form where the form is validating with jquery. Now in this form there is radio button list. If the user clicks yes then textbox related to that radio button list will not be validated. If the user clicks no then textbox related to that that radio button list should be validated..

But now the problem is that the developer before me validates the form like this:

jQuery(".required").each(function(){
    var vlr = jQuery(this).val();
    if(vlr == ''){
        alert("Value not to be blank");
        jQuery(this).focus();
        e.preventDefault();
        e.stopPropagation();
        return false;
    }
});

Html part of radio button list

<tr>
   <td >Is premium paid? 
     <input name="is_premium_016" value="yes"  type="radio">Yes
     <input name="is_premium_016" value="no"  type="radio">No
   </td>
   <td >if not,why not? <input name="if_not_017" type="text" ></td>
  </tr>

This code is validating the complete form how can I catch the radio button list value and how can I stop validation of the textbox if user ticks yes?

I hope you understand what I want.

Thanks

203

Answer

Solution:

Try this :

$('#search_form').submit(function() {
    if($('#fepUserIDValue').attr("checked") == "checked" && $('#inquiry_date').val() == '') {
       alert('Required field missing');
       return false;
    }
});

Reference links :

jQuery Validation ONLY when certain radio buttons selected

How do I Required text box if radio button selected on form submission using jquery?

588

Answer

Solution:

HTML

   <input type="radio" name="check" > yes: <br>
   <input type="radio" name="check" > no: <br>

  <div>
    <input type="text" name="name"> 
  </div>

JQUERY

      <script>

     //select the  radio buttons to be clicked 
    var radioBtns = jQuery('.checker');
     //text box to be affected but the radio buttons
     var textbox  = jQuery('.isValid');

     //when any of the  buttons  is clicked  a function to validate the textbox is run  
     radioBtns.on('click',function(){
        // get the button clicked 
         if (jQuery(this).hasClass('yes')){
             // put code to affect textbox when not valid
             alert('textbox not valid');
         }else if(jQuery(this).hasClass('no')){
             //put code to affect the textbox when it is valid 
             alert('text box is valid ');
         }

     });
 </script>
380

Answer

Solution:

So you want to Perform your Validation only if the User clicks on No? Is that correct? Well here is a Code that might work for you: assuming the Assumption above is right.

JAVASCRIPT

        <script type="text/javascript">
            ;                           // CLOSE OFF ANY UNCLOSED JS TAGS... IF ANY ;-)
            jQuery.noConflict();        // ADD THE noConflict() METHOD, IN CASE YOU ARE USING OTHER LIBRARIES THAT MAKE USE OF THE $ SYMBOL
            (function ($) {
                $(document).ready(function(e) {
                    // CREATE VARIABLES TO HOLD THE FORM, RADIO-BUTTON & TEXT-FIELD JQUERY OBJECTS
                    var isPremiumRadio      = $("input[name=is_premium_016]");
                    var ifNotWnyNot         = $("input[name=if_not_017]");
                    var controlForm         = $("form[name=name_of_your_form]");    //<== PLEASE; PUT THE REAL NAME OF YOUR FORM IN THE STEAD OF "name_of_your_form"

                    controlForm.on("submit", function(evt){
                        var theForm         = $(this);
                        var invalids        = [];
                        evt.preventDefault();
                        evt.stopPropagation();

                        $(".required").each(function(){
                            var vlr = jQuery(this).val();
                            if( $(this).is(isPremiumRadio) ){
                                if(isPremiumRadio.val() == "no"){
                                    //PERFORM YOUR VALIDATION HERE...
                                    if(ifNotWnyNot.val() == ""){
                                        invalids.push("Blank Field Error");
                                        alert("Could you, please, let us know why not?");
                                    }
                                }
                            }else if( $(this).is(ifNotWnyNot)  && ifNotWnyNot == ""){
                                if(isPremiumRadio.val() == "no"){
                                    invalids.push("Blank Field Error");
                                    alert("Could you, please, let us know why not?");
                                }
                            }else{
                                // VALIDATE OTHER FIELDS OTHERWISE...
                                if(vlr == ''){
                                    invalids.push("Blank Field Error");
                                }
                            }
                        });

                        if(invalids.length == 0){
                            theForm.submit();
                        }else{
                            alert("Value not to be blank.");
                        }

                    });
                });


            })(jQuery);

        </script>

HTML ++FORM++

    <tr>
      <td >Is premium paid?
        <input name="is_premium_016" value="yes"  type="radio">Yes
        <input name="is_premium_016" value="no"  type="radio">No
      </td>
      <td >if not,why not? <input name="if_not_017" type="text" ></td>
    </tr>

People are also looking for solutions to the problem: php - zabbix frontend error mysql_connect, init_set

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.