Making email field required in php [closed]

▼魔方 西西 提交于 2019-12-02 13:42:11

You have to check values of $_POST['firstname'], $_POST['lastname'] and $_POST['email']

For the the name, you can check it with :

empty()

if ( empty($_POST['firstname']) || empty($_POST['lastname']) )
  // catch error

You can also, use strlen() and trim() to check string size and not validate a name with only 1 character length.

For email, you can check it with :

filter validate

if ( !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) )
  // catch error
if(filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {
    //Your code
} else {
    //Show your errors
}

The above code will validate the contents of your e-mail post variable to be a valid email address.

Here's the best way to do this in PHP.

<?php
if(isset($_POST["email"])){
//Perform action
}
else{
echo "Please type in your email";
}
?>`

This should do the trick.

You don't have to do this in PHP anymore. In your form where you have your email filed/textbox, just insert required and type as email then the form won't submit unless this fill contains a valid email address.

<input type="email" name="email" id="email" required value="<? echo $email; ?>" placeholder="example@fordberg.com"/>

That should do the trick. HTML5 Baby!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!