Results 1 to 4 of 4

Thread: Radio button validation

  1. #1
    Join Date
    Jul 2006
    Posts
    142
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Radio button validation

    I got this code from javascriptkit. And I added a few code to validate the radio button and checked boxes. It works for check boxes but not for radio buttons

    Code:
    function checkrequired(which){
    
    var btn = valButton(document.Reg.race);
    
    var pass=true
    if (document.images){
    for (i=0;i<which.length;i++){
    var tempobj=which.elements[i]
    
    if (tempobj.name.substring(0,8)=="required"){
    
    //All the document.Reg.box<number>.checked  == false are to check for check boxes
    
    if (((tempobj.type=="text"||tempobj.type=="textarea")&&tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&tempobj.selectedIndex==-1) || (document.Reg.box1.checked == false && document.Reg.box2.checked == false && document.Reg.box3.checked == false && btn == null )){
    pass=false
    break
    }
    }
    }
    }
    if (!pass){
    alert("One or more of the required elements are not completed. Please complete them, then submit again! Please Make sure you select at least one checkbox")
    return false
    }
    else
    return true
    }
    
    //To validate radio buttons
    
    function valButton(btn) {
        var cnt = -1;
        for (var i=btn.length-1; i > -1; i--) {
            if (btn[i].checked) {cnt = i; i = -1;}
        }
        if (cnt > -1) return btn[cnt].value;
        else return null;
    }
    Help please !

    Thanks

  2. #2
    Join Date
    Jul 2008
    Posts
    128
    Thanks
    0
    Thanked 17 Times in 16 Posts

    Default

    Quote Originally Posted by neo_philiac View Post
    I got this code from javascriptkit. And I added a few code to validate the radio button and checked boxes. It works for check boxes but not for radio buttons
    Checkboxes and radio buttons are read in the same way. Remember that if there is only one element in a group, then the .length property does not exist.

  3. #3
    Join Date
    Jul 2006
    Posts
    142
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    Does that mean I am doing it right ?

    I have 2 in a group!

    Code:
        <td width="314" height="24"> <span class="body">
        <Input type = 'Radio' Name ='race' value= 'Male'>Male
    	<Input type = 'Radio' Name ='race' value= 'Female'>Female<br>
        </span>
    Thanks

  4. #4
    Join Date
    Jul 2008
    Posts
    128
    Thanks
    0
    Thanked 17 Times in 16 Posts

    Default

    Quote Originally Posted by neo_philiac View Post
    Does that mean I am doing it right ?

    I have 2 in a group!

    Code:
        <td width="314" height="24"> <span class="body">
        <Input type = 'Radio' Name ='race' value= 'Male'>Male
    	<Input type = 'Radio' Name ='race' value= 'Female'>Female<br>
        </span>
    Thanks
    Have you alerted the value returned by the function? If it's as expected, you'll know to look elsewhere.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •