Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

help with regex

Featured Replies

<?php

$email = $_POST['email'];

$regex = '/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/';

 

if (preg_match($regex, $email)) {

echo $email . "is a valid email.";

} else {

echo $email . "is an invalid email.";

}

 

?>

<form action="try.php" method="post" />

Email<input type="text" name="email">

<input type="submit" name="submit" />

</form>

 

http://bkwebdesignandhosting.co.uk/php/loginsystem/try.php

 

I need to make it so the valid/invalid notice only appears after pressing submit, atm its always there, the code does work though :)

Just use an IF statement to check if $email has a value.

 

On a side note I changed the regex to make it more effective validating an email, making sure that it is invalid when no domain is entered, e.g. it will now be invalid when they enter an email like "example@example" unlike before

 


<?php
$email = $_POST['email'];
$regex = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
if(!empty($email))  {
 if (eregi($regex, $email)) {
   echo $email . " is a valid email.";
 } 
 else {
   echo $email . " is an invalid email.";
 }
}
?>
<form action="try.php" method="post" />
Email<input type="text" name="email">
<input type="submit" name="submit" />
</form>

Edited by Barth

  • Author

thanks for cleaning up my regex code

 

also could you please exsplain what this line does so i can apply it to other things :)

 

if(!empty()

if(!empty($variable))

In human

(Is the variable not empty?)

 

It is basically checking if the variable contains something

 

It can also be done in different ways, such as these

 

if($variable!= null)
if($variable!= "") 

 

Hope this helps :)

On a side note I changed the regex to make it more effective validating an email, making sure that it is invalid when no domain is entered, e.g. it will now be invalid when they enter an email like "example@example" unlike before

The problem with this is that email addresses just like that will be perfectly valid soon enough, when ICANN allow any TLD from 2012 (provided someone will have the infrastructure to support the TLD)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.