Home > OS >  HTML5 email pattern attribute for my school email
HTML5 email pattern attribute for my school email

Time:09-13

I want to make user only enter my school email using pattern attribute. The pattern should look like "[email protected]". XXXXX could be letters (lowercase, uppercase ) or numbers. I have the first part: [a-zA-Z0-9]{5}. I am not sure how to add @auburn.edu

    <div  style="margin-top: 50px;">
      <form method="POST" action="index.php">
            <div >
            <label for="inputAUEmail" >Auburn Email*</label>
            <div >
            <input type="text"  id="student_email" name="student_email" placeholder="[email protected]" required pattern="[a-zA-Z0-9]{5}">
            </div>
            </div>
    
        <div >
          <div  style="margin-top: 10px;">
                <button type="submit"  name="submit">Submit</button>
              </div>
        </div>
    </form>
</div>

CodePudding user response:

You can use the following regex expression:

Expression:

pattern="[a-zA-Z0-9]{5}@(auburn\.edu)$"
  • @ = value must have '@' char in it
  • (auburn.edu) = regex group for a specified string
  • $ = to tell end of string

CodePudding user response:

Since you have "php" tag on question, I think this method you might can try.

$email = isset($_POST['email']) ? trim($_POST['email']) : null;

// List of allowed e-mail domains
$allowdomain = [
    'auburn.edu'
];

// To check  the address is an valid address
if (filter_var($email, FILTER_VALIDATE_EMAIL))
{
    // Separate email by "@" characters ( As there should be only one "@")
    $parts = explode('@', $email);

    // Removes and return the domain
    $domain = array_pop($parts);

    // Check if the domain is in our list
    if ( ! in_array($domain, $allowdomain))
    {
        // Not allowed. So add your not allowed output msg or alert here
    }
}

domain means "@auburn.edu" on your question. "allowdomain" is an array to put any of those domains that need to check. If you need to add more domains, it should be look like,

$allowdomain = [
    'auburn.edu',
    'site.ca',
    'site2.us'
];

CodePudding user response:

In a regular expression, anything that doesn't have a special meaning just means itself. So the pattern for exactly the word "auburn" is simply auburn. The @ character also has no special meaning, so that's easy too.

However, a . does have a special meaning (it means "any single character"), so you have to "escape" it as \.

Putting that on the end of your existing pattern is then really simple:

pattern="[a-zA-Z0-9]{5}@auburn\.edu"

Note: client-side for convenience; server-side for security. The validation provided in the browser is extremely trivial for anyone to ignore; it's helpful to spot mistakes, because it's fast, but it's completely useless for actually being certain.

To check on the server, you need to check the same thing in PHP, using preg_match, e.g.

if ( ! preg_match('/^[a-zA-Z0-9]{5}@auburn\.edu$/', $_POST['student_email'] ) {
    echo 'E-mail address not allowed!';
    exit;
}

Note the extra ^ and $ which match "beginning of string" and "end of string", so that the pattern rejects things like "[email protected]" which only contain the required address.

  • Related