Click here to Skip to main content
11,711,750 members (80,090 online)
Click here to Skip to main content

Tagged as

Validating gender dynamically using REGEX

, 19 Apr 2012 CPOL 7.3K
Rate this:
Please Sign up or sign in to vote.
Validating gender dynamically using REGEX

Introduction

This is to show how we can validate a gender using REGEX. We can validate whether the gender entered is Male or Female using this code. 

Using the code

The following code illustrate how we can check for gender validation 

protected void btnGender_Click(object sender, EventArgs e)
    {
        if (chkGender(txtGender.Text))
        {
            lblGender.Visible = true;
            lblGender.Text = txtGender.Text;
        }
        else
        {
            lblGender.Visible = true;
            lblGender.Text = "Not a valid Gender";
        }
    }
    public bool chkGender(string strGender)
    {
        bool m_flag = false;
        Match match = Regex.Match(strGender, @"^M(ale)?$|^F(emale)?$");
        bool result = match.Success;
        if (result)
        {
            m_flag = true;
        }
        else
        {
            m_flag = false;
        }
        return m_flag;
    } 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtGender" runat="server"></asp:TextBox>
        <asp:Button ID="btnGender" runat="server" OnClick="btnGender_Click" Text="Validate" />
    </div>
    <br />
    <asp:Label ID="lblGender" runat="server" Visible="false"></asp:Label>
    </form>
</body>
</html>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

demouser743
Software Developer
India India
I am Dorababu, working as a Software engineer. Web development in Asp.Net with C#, WinForms and MS sql server are the experience tools that I have had for the past 3 years. Yet to work on WCF, WPF, Silverlight and other latest ones.

You may also be interested in...

Comments and Discussions

 
QuestionPoor Pin
Míša Hájková25-Apr-12 3:52
memberMíša Hájková25-Apr-12 3:52 
Suggestion[My vote of 2] Over complicated code Pin
Matt T Heffron19-Apr-12 7:49
memberMatt T Heffron19-Apr-12 7:49 
All of the chkGender method can be replaced with two lines of code:
// This once:
private static Regex ValidGender = new Regex(@"^M(ale)?$|^F(emale)?$");
// This instead of if (chkGender(txtGender.Text))
if (ValidGender.IsMatch(txtGender.Text))
This will be more efficient.
To the Regex constructor you can add the RegexOptions.IgnoreCase for a case-insensitive compare.

Ultimately, it would be even simpler to use Radio Buttons for Male and Female options on the page so the user doesn't need to type anything, and input cannot be invalid.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 19 Apr 2012
Article Copyright 2012 by demouser743
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid