简体   繁体   中英

Radio Button Validation u

I am trying validate the radio button using Javascript . But I couldn't get it. Can any one please help me to fix this Issue. I Have attached My Code Below. Thanks.

<form action="submitAd.php" method="POST" enctype="multipart/form-data" name="packages" onsubmit="return checkForm()">
    <div id="plans_pay">
            <input  type="radio" name="group1" id="r1" value="Office"  onchange="click_Pay_Office()"  style="float:left;margin-top:20px;font-size:72px;">
            <label style="float:left; margin-top:20px;" for="pay_office">At Our Office</label>
            <img style="float:left;margin-bottom:10px;"  src="images/Pay-at-office.png" />
          </div>
      <div id="plans_pay">
                    <input style="float:left;margin-top:20px;font-size:72px;" type="radio" name="group1" id="r2" value="HNB"  onchange="click_Pay_Hnb()">
                <label style="float:left; margin-top:20px;" for="pay_hnb">At Any HNB Branch</label>
               <img style="float:left;margin-bottom:10px;"  src="images/HNB.png" />
        </div>
</form>

Javascript

function checkForm(){


    if( document.packages.pso.checked == false && document.packages.pso1.checked == false && document.packages.ph.checked == false && document.packages.ph2.checked == false && document.packages.ph3.checked == false  && document.packages.pl.checked == false && document.packages.p3.checked == false && document.packages.p4.checked == false && document.packages.p5.checked == false && document.packages.p6.checked == false ){

        alert('Please Select At Least One Package');
        return false;

    }
    if(  document.packages.pso.checked == false && document.packages.pso1.checked == false && document.packages.ph.checked == false && document.packages.ph.checked == false && document.packages.ph2.checked == false && document.packages.ph3.checked == false  && document.packages.pl.checked == false && document.packages.p3.checked == false  && document.packages.p4.checked == false  && document.packages.p5.checked == false && document.packages.p6.checked == false){

        alert('Please Select At Least One with the Advertise online option in premium package');
        return false;

    }
    if(document.getElementById('words').value==''){
        alert("Please Enter the Texts");
        return false;

    }

    if(document.getElementById('r1').checked==false && document.getElementById('r2').checked==false){
        alert("Please Select a Payment Method");
        return false;

    }


}

Instead of calling the function checkForm() in form tag on event onsubmit , better use it in submit button

<input type="submit" name="submit" value="submit" onClick="return checkForm()" />

It will work better and outputs as required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM