poqoz-87 Posted May 15, 2007 Share Posted May 15, 2007 i m trying to validate radiobutton n checkbox but to no avail. this is the code that i m currently using in validating the radiobutton <script language="JavaScript"> function checkForm() { var cgender with(window.document.msg) { cgender = gender; } if(trim(cgender.value) == '') { alert('Please select ur gender); cgender.focus(); return false; } else { cgender.value = trim(cgender.value); </script> <form name="msg" id="msg"> Gender: <input name="gender" type="radio" value="male" /> Male <input name="gender" type="radio" value="female" /> Female <input type="submit" name="submit" value="Submit"onClick="return checkForm();"> i have try using the above code to validate my radiobutton but it does not work at all. can someone tell me what went wrong in my code n also, how to validate check box n dropdown list? Quote Link to comment Share on other sites More sharing options...
simon551 Posted May 15, 2007 Share Posted May 15, 2007 check to make sure you have all your {} pairs right, for starters. Quote Link to comment Share on other sites More sharing options...
xenophobia Posted May 17, 2007 Share Posted May 17, 2007 Hehe... I got a silly answer or perhaps a suggestion. Why you don't make default checked? Like <input type="radio" name="gender" value="male" checked /> <input type="radio" name="gender" value="female" checked /> So you don't have to worry that user had entered the value or not. Okay now for drop-down list: let say you got this: <select name="dropdownlist"> <option value="-1">Please select one</option> <option value="php">PHP</option> <option value="mysql">MySQL</option> . . . //so on.... </select> //so in your code. with(window.document.msg) { var drop_down_list_value = dropdownlist.options[dropdownlist.selectedIndex].value; if(drop_down_list_value == -1){ alert("Please select your favorite language!"); } } Hope this helps you! Quote Link to comment Share on other sites More sharing options...
shank888 Posted May 17, 2007 Share Posted May 17, 2007 dunno if this would doo much <input type="submit" name="submit" value="Submit"onClick="return checkForm();"> add a space after "submit <input type="submit" name="submit" value="Submit" onClick="return checkForm();"> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.