I have a form and want to verify it with javascript/jquery.
One of the fields in the form is the Gender with has two radio buttons (Male and Female)
<input type="radio" name="gender" id="genderMale" onblur="verifyGender()">
<label for="genderMale">Male</label>
<input type="radio" name="gender" id="genderFemale" onblur="verifyGender()">
<label for="genderFemale">Female</label>
I want to verify the gender by leaving them (using onblur
).
function verifyGender()
{
if(document.getElementById("genderMale").checked==true || document.getElementById("genderFemale").checked==true)
{
// success
}
else
{
// display error message
}
}
I got a little bug if the user uses his/her keyboard to access the <input>
fields. When the user is female, she could press tab key to go to the gender.
While the user keeps on pressing tab until it reaches to gender, the Male radio button is focused first. Since she is not male, she presses tab again and press space or just pressing the right arrow key to select "Female" but the error is already displayed.
So, I decided to execute only the function after the user leaves the gender field but I don't know how. How should I do it?
The error message must not displayed when the user is still on the gender radio buttons and he/she doesn't select one yet.