Results 1 to 6 of 6

Thread: how to validate radiobutton, checkbox n dropdown list?

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2006
    Posts
    58

    how to validate radiobutton, checkbox n dropdown list?

    i am trying to validate radiobutton but to no avail.

    this is the code that i m currently using to validate 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);
    return true;
    }
    }
    </script>
    <body>
    <form method="post" name="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();">

    when i click onto the submit button, nothing happens.
    can someone tell me what is wrong with my code? n can someone give me an example on how to validate checkbox and also dropdown list?

  2. #2
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: how to validate radiobutton, checkbox n dropdown list?

    You shouldn't need to validate a radio option its either going to be one or the other just include the checked attribute for one of the options
    HTML Code:
    <input name="gender" type="radio" value="male" checked="true"/>

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2006
    Posts
    58

    Re: how to validate radiobutton, checkbox n dropdown list?

    i want it to validate. so that when i click onto the submit button, there will be pop-up message that says "Please select your gender"

  4. #4
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: how to validate radiobutton, checkbox n dropdown list?

    In that case cgender is an array so check
    Code:
    if(cgender[0].checked == false && cgender[1].checked == false){
    
    }

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2006
    Posts
    58

    Re: how to validate radiobutton, checkbox n dropdown list?

    when i tried this code, there is a pop up error msg that say "please...gender" then it quickly redirect me to another page.

    this is the code that i m doing right now. it did not validate the name validation part but instead, just redirect me to another page. can someone shine the light here? i want it to validate the name as well.
    function checkForm()
    {
    var gender, fname;
    with(window.document.msgform)
    {
    gender=ngender;
    name = nfname;

    if(gender[0].checked == false && gender[1].checked == false)
    {
    alert('Please specify your gender');
    return false;
    }
    else if(fname.value == "")
    {
    alert('Please enter your first_name');
    fname.focus();
    return false;
    }
    else{
    return true;
    }
    }
    }
    <form name = "form" onsubmit="return checkform();">
    <input name="gender" type="radio" value="male" />
    Male
    <input name="gender" type="radio" value="female" />
    Female</label>
    First Name: <input name="name" type="text"/>
    <input type="submit" name="submit" value="Submit">

  6. #6
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: how to validate radiobutton, checkbox n dropdown list?

    You might have to look at your code thoroughly. Variables were declared wrong, your form name is different, and there's no even a method and action. Here is the revised version:

    Code:
    <script>
    function checkForm()
    {
    var ngender, fname;
    with(window.document.msgform)
    {
     ngender = gender;
     fname = name;
    
     if(ngender[0].checked == false && ngender[1].checked == false)
     {
     alert('Please specify your gender');
     return false;
     }
    
     else if(fname.value == "")
     {
     alert('Please enter your first_name');
     fname.focus();
     return false;
     }
     else{
           return true;
           }
    }
    }
    </script>
    <form name ="msgform" method=POST onsubmit="return checkForm();">
    <input name="gender" type="radio" value="male" />
    Male
    <input name="gender" type="radio" value="female" />
    Female</label>
    First Name: <input name="name" type="text"/>
    <input type="submit" name="submit" value="Submit">

Posting Permissions

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



Click Here to Expand Forum to Full Width