GiveChance GiveChance - 17 days ago 6
jQuery Question

Validation for gender

var Surname1 = $('#sn1').val();
var Name1 = $('#n2').val();
var Gender1 = $('#g3').val();
var Address1 = $('#a4').val();
var Email1 = $('#em5').val();
var Phone1 = $('#hp6').val();
var emailcheck = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
var phonecheck = /^\d{8}$/;
var gendermcheck = /^[Male]$/;
var genderfcheck = /^[Female]$/;

if (Surname1 == "" || Name1 == "" || Gender1 == "" || Address1 == "" || Email1 == "" || Phone1 == "")
{
alert("Updating failed! Please try again!!!");
return false;
}
else if (!emailcheck.test(Email1))
{
alert("Unable to Update! Please Check your Email Address");
return emailcheck.test(Email1);
}
else if (!phonecheck.test(Phone1))
{
alert("Unable to Update! Please Check your contact number");
return phonecheck.test(Phone1);
}
else if (!genderfcheck.test(Gender1) || gendermcheck.test(Gender1))
{
alert("Type Male/Female");
return genderfcheck.test(Gender1);
return gendermcheck.test(Gender1);
}
else
{
alert("Update was Successful! Thank you for updating our system!");
}


Validate Male and Female If i would want to make sure that users can only type Male or Female in the box to save. If they typed male it will prompt an error is there any ways for me to code it so that it can only accept "Male" or "Female"?

Answer

As others suggested, if you have a pre-defined list of options, you should use a radio button or dropdown list.

But to answer your question, if you want the user to type ONLY either Male or Female (case sensitive), you should change their corresponding regular expressions like this:

var gendermcheck = /^Male$/;
var genderfcheck = /^Female$/;
Comments