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? Link to comment https://forums.phpfreaks.com/topic/51469-how-to-validate-radiobutton-checkbox-and-dropdown-list/ 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. Link to comment https://forums.phpfreaks.com/topic/51469-how-to-validate-radiobutton-checkbox-and-dropdown-list/#findComment-253727 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! Link to comment https://forums.phpfreaks.com/topic/51469-how-to-validate-radiobutton-checkbox-and-dropdown-list/#findComment-255346 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();"> Link to comment https://forums.phpfreaks.com/topic/51469-how-to-validate-radiobutton-checkbox-and-dropdown-list/#findComment-255676 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.