0

I have done a test for a gender expression -

function gender()
{
    var gender = document.form1.radio[0].checked;
    var gender1 = document.form1.radio[1].checked;  
        if(gender || gender1)
        {
        }
        else
        {
            errorMsg = errorMsg + "please select your gender\n" 
        }
}

but I would like to be able to write it so that there is no need for an empty positive outcome like this -

if ((alphabetic.test(fname)== false) || (alphabetic.test(lname)== false))
{
alertmsg = alertmsg + "Name should be in alphabets:" + "\n";
}

I am sorry if I appear to be very stupid, I am a complete beginner. any help would be appreciated, thanks

5
  • what was your question again? Commented Feb 15, 2013 at 17:22
  • 2
    Please do not use checkboxes for gender input. It is not fair: intersexroadshow.blogspot.de/2012/02/… Commented Feb 15, 2013 at 17:23
  • I would also, for clarity, maybe name your variables after the actual gender as opposed to gender and gender1. Also a great programmer would account for the possibility of new genders :|. Commented Feb 15, 2013 at 17:23
  • I was told on my course that my testing code was too complex and that I should re-write it so there is no need for empty positive outcomes and that I should use the same testing method like the first and last name. I'm not sure what they mean Commented Feb 15, 2013 at 17:25
  • sorry, yes I'll the gender for something else Commented Feb 15, 2013 at 17:30

3 Answers 3

1
function gender()
{
var gender = document.form1.radio[0].checked;
var gender1 = document.form1.radio[1].checked;  
    if(!(gender || gender1))
    {
        errorMsg = errorMsg + "please select your gender\n" 
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

If I understand correctly:

if(!gender && !gender1) {
    errorMsg = errorMsg + "please select your gender\n" 
}

3 Comments

function gender() { var gender = document.form1.radio[0].checked; var gender1 = document.form1.radio[1].checked; if(!gender && !gender1) { errorMsg = errorMsg + "please select your gender\n" }
this would make sure they have selected at least one of the 2 radio buttons?
@user2073133 Yes, it will. If none is selected, the final result will be true, and the error message will be appended. In any other case, the final result will be false.
0

Not really sure what you are trying to do but, try using the logical NOT "!":

function gender()
{
    var gender = document.form1.radio[0].checked;
    var gender1 = document.form1.radio[1].checked;  
    if !(gender || gender1)
    {
        errorMsg = errorMsg + "please select your gender\n" 
    }

}

1 Comment

I just need a test to check that at least one gender is selected. A Test that is better than my original one, thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.