Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 493423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T02:12:49+00:00 2026-05-13T02:12:49+00:00

I know there are more elegant ways to set up an html form to

  • 0

I know there are more elegant ways to set up an html form to take the input of male or female; however, I am learning, and I was just wondering if this is also possible:

//Incorporate two checks
 if (empty($_POST['sex'])==FALSE && sanityCheck($_POST['sex'], 'string', 1)==TRUE)
      {
      // if the checks are ok for  sex we assign sex  to a variable
      $sex = $_POST['sex'];
       }
      else
      {
      // if all is not well we echo an error message
      echo 'Please enter a valid Gender';
      // and exit the script
      exit();
      }

If so, how would I check this with regex?
Whether the user typed M or F.

I am thinking:

function checksex($sexcheck)
{
//Some regex here to check the sex?
  return preg_match('               ', $sexcheck);
}

And then call checksex as a third check added to the if conditional, like this:

if (empty($_POST['sex'])==FALSE && sanityCheck($_POST['sex'], 'string', 1) != FALSE && checksex($_POST['sex'], 1) ==TRUE)
{
...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T02:12:49+00:00Added an answer on May 13, 2026 at 2:12 am

    To check if the user typed M or F, all you would need to do is a string comparison. No need for regex

    if ( isset($_POST['sex']) ){
        $sex = $_POST['sex'];
        if ( $sex == 'M' ){
            echo 'Male';
        }else if ( $sex == 'F' ){
            echo 'Female';
        }else{
            echo 'Please enter a valid Gender';
        }
    }else{
        echo 'Please enter a valid Gender';
    }
    

    However, rather than having the user type out M or F, why not use a select box or Radio Buttons ?

    A select box may look something like this

    <select name='sex'>
        <option selected="yes" value="NA">Not Saying</option>
        <option value="F">Female</option>
        <option value="M">Male</option>
     </select>
    

    Or radio buttons:

    <input type="radio" name="sex" value="M" /> Male <br />
    <input type="radio" name="sex" value="F" /> Female
    

    You would of course still have to check that $_POST['sex'] is still M or F, and handle the case where it isn’t either of them, as the user may decide to post an invalid value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that there are more practical ways of going about what I'm trying
As far as I know there is not a significantly more elegant way to
I know there has got to be a cleaner more elegant way to do
I know there is this post , but I still want to know more
I'm not too good with SQL and I know there's probably a much more
I know and use eric meyer CSS reset, but is there any more things
In jQuery ajax function there is xhr option. Does someone know more details, usability
I know well that there are ways of customizing elements like GridView and ListView
I'm making an input that looks like this: There are many different ways to
As a matter of general interest I'm wondering if there's a more elegant/efficient way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.