1

On a webshop, I need to check if the user has checked this payment choice, and if he checked the CGU rules.

So this my html :

    <div id="choixPaiement">
      <div class="choixDuReglement">
        <ul>
            <li><input type="radio" name="buttonModePaiement" value="tata"></li>
            <li><input type="radio" name="buttonModePaiement" value="toto"></li>
            <li><input type="radio" name="buttonModePaiement" value="titi"></li>
            <li><input type="radio" name="buttonModePaiement" value="tutu"></li>
        </ul>
      </div>
      <div id="accepte_cgv">
        <p> <input type="checkbox" name="check_cgv" id="check_cgv"><label>Lu et approuvé</label></p>
        <p><a onclick="validCommande();" class="btnValidCommande" href="javascript:;"><span>Valid my order</span></a></p>
      </div>
    </div>

And this is my javascript :

function validCommande(){
  var paiement = $("input[@name=buttonModePaiement]:checked").val();
  alert(paiement);
  if(paiement == "undefined"){
    alert('please choose payment method');    
  }
  alert($('#check_cgv').is(':checked'));

}

when radiobutton and checkbox are not checked, I get two alert message : "undefined" and "false" (so that is correct).

But when I'm checking the checkbox, and no radiobutton, I get it : "on" and "true".

Why ?

1
  • To check for undefined, you should use typeof variable === 'undefined'. Commented Dec 14, 2011 at 16:09

3 Answers 3

1

I think this is what you want

function validCommande() {
    var paiement = $("input[name='buttonModePaiement']:checked");

    if(paiement.length === 0)
      alert('please choose payment method');    
    else
      alert(paiement.val());

    alert($('#check_cgv').is(':checked'));
}

Also, you're not closing your input tags:

<input type="radio" name="buttonModePaiement" value="tata" />
Sign up to request clarification or add additional context in comments.

3 Comments

@Adam Rackis, You missed a closing curly bracket.
@ptriek - fair enough. Visual studio complained to me when I actually tested the code, but then again visual studio complains every time I add a data-foo attribute.....*sigh*
+1 Checking the length is the proper way to do it since jQuery returns an array of matching elements, where in this case, if no items are checked, then you'll have an empty array.
0

Here is what I would do to your code and why:

<div id="choixPaiement">
      <div class="choixDuReglement">
        <ul>
            <li><input type="radio" class="buttonModePaiement" value="tata"></li>
            <li><input type="radio" class="buttonModePaiement" value="toto"></li>
            <li><input type="radio" class="buttonModePaiement" value="titi"></li>
            <li><input type="radio" class="buttonModePaiement" value="tutu"></li>
        </ul>
      </div>
      <div id="accepte_cgv">
        <p> <input type="checkbox" name="check_cgv" id="check_cgv"><label>Lu et approuvé</label></p>
        <p><a id="btnValidCommande" href="#"><span>Valid my order</span></a></p>
      </div>
</div>

$(document).ready(function(){
    $('#btnValidCommande').click(function(e){
        e.preventDefault();  // stop the link from trying to navigate
        validCommande();
    });
});

function validCommande() {
    var paiement = $(".buttonModePaiement:checked").val();

    if (!paiement) {
        alert('please choose payment method');
    }
    alert($('#check_cgv').is(':checked'));
}
  • Use classes when repeating identifiers, not names. Names are supposed to be unique for things like form submission. name="buttonModePaiement" should be class="buttonModePaiement".

  • Append your events in JS code, not inline. Notice I removed the onclick="validCommande();" from your link and append it in the DOM ready event.

  • You don't need to (nor should you) compare against "undefined". This will suffice (paiement == "undefined") ---> (!paiement)

Working fiddle: http://jsfiddle.net/hMdKT/3/

1 Comment

thanks for these information, it's really "proper" that my JS
0

if u want to validate the radio button the do this way

<form name="as" method="post" action="n.php">
<input type="radio" id="x1"   name="red">
<input type="radio"  id="x2"   name="red">
<input type="radio"   id="x3"  name="red">
<input type="radio"   id="x4"  name="red">

<input type="submit" value="button" onclick="return xyz()">
</form>

    function xyz()
{
    var x = document.getElementsByName("red");
    for (var i=0; i<x.length; i++)
    {
        if (x[i].checked) {
            return true;
        }
    }
    // No radio button checked, return false.
    alert("hello");
    return false;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.