Jump to content

how to validate radiobutton, checkbox and dropdown list?


poqoz-87

Recommended Posts

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
Share on other sites

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.  :D

 

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.