mercredi 12 avril 2017

Regex Visual Studio One Specific Character

I am trying to validate a text box in Visual Studio to be either the character M or F. I have a few other validations with regex set in a similar fashion which are all working correctly. However, with this one, it still allows me to use ANY letter in the text box. What is wrong with this code?

public static bool IsGender(string gender)
    bool validGender = true;
    string _genderRegEx = @"^\:|m|M|f|F|$";
    if ((!Regex.Match(gender, _genderRegEx).Success))
        MessageBox.Show("Gender must be either M or F.");
        validGender = false;
    return validGender;

Aucun commentaire:

Enregistrer un commentaire