SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    can anyone show me on how to validate a radio button and a check box?

    hi

    can anyone please teach me or give me an example on how to validate a radio button/ check box? what i want to do is on submit of the page, alert if the radio or checkbox is empty or not selected,just like a text validation if it is empty,i tried using a script on validating a textbox but it doesn't work in radio and checkbox.I'll really appreciate it anyone can help me,tnx in advance!

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code javascript:
    function frmValidate(frm){
    	if(frm.sex[0].checked == false && frm.sex[1].checked == false){
    		alert('Please select sex.');
    		return false;
    	}
    	else if(frm.chkBox.checked == false){
    		alert('Please select education.');
    		return false;
    	}
    	else{
    		return true;
    	}
    }
    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Untitled Document</title>
    </head>
    <body>
    <form name="form1" method="post" action="" onSubmit="return frmValidate(this);">
      <p>Sex
        : 
        <input type="radio" name="sex" id="radio" value="Male">
      Male
      <input type="radio" name="sex" id="radio2" value="Female">
      Female  </p>
      <p>
        Education : 
        <input type="checkbox" name="chkBox" id="chkBox">
        Graduate  </p>
      <p>
        <input type="submit" name="button" id="button" value="Submit">
          </p>
    </form>
    </body>
    </html>
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


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
  •