I need to check if the email the user enters during registration is valid/real, meaning they can't just enter whatever they want as long as it meets the proper email format, I need to actually be able to send mail to it.
I also don't want to do email confirmation, though, where the user would have to click a link from an email to register. I just want it to verify on the spot.
You can do all the validation you want, but in the end the best and only way is to send an email with a verification link.
Also, because there is a huge amount of valid and odd emails that you would think aren't, the idea is generally to have fairly loose validation. Take a look here for all sorts of interesting emails.
You will need to do a DNS lookup on the MX record for the domain, then make sure that mx domain is contactable. I think that is about as close as you can get.
You might also consider letting a 3rd party do the validation for you - e.g. implement Google OAuth as a method of registration for your site.
In case of Laravel (you've mentioned it as a tag for the question) I suggest you to use such a Laravel's native feature as Validation: Laravel 3 / Laravel 4.
Below is how I use it in POST controller:
// Rules for form validation
$rules = array(
'email' => array('required', 'email', 'unique:users,email'), // Email is required and should satisfy E-mail format and it should be unique for table users.
);
// Form validation
$validation = Validator::make(Input::all(), $rules);
if($validation->fails()) {
// Flash validator with input values and errors to some controller
Former::withErrors($validation);
return Redirect::to_action('some.controller')
->with_input()
->with_errors($validation);
}
}
In general PHP usage case you can check it using PHP's native function like this:
filter_var($email, FILTER_VALIDATE_EMAIL)
Related
I would like to make a user registration form which only accepts email addresses from my university.
e.g
me#gmail.com, me#yahoo.com, me#live.com = INVALID
me#university.ac.uk = VALID
what types of terms and queries should I start researching to achieve this? I am new to PHP, but from the homework, I've done so far can only find tutorials on how to validate emails based on rejecting invalid inputs, rather than only accepting valid domain extensions such as #university.ac.uk
Hope this makes sense, not trying to get code written for me (although visual explanations are helpful), just asking for a few pointers to help get in the right direction.
i will suggest you regex for email that your requirements.
/^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#\buniversity.ac.uk/
Please visits here https://regex101.com/r/KkJeho/9
If you first make sure the address is an email, and then explode() the email, you can check if the server matches:
<?php
$email = "foo#university.ac.uk";
$allowed_servers = array("university.ac.uk", "other_server.maybe");
if (filter_var($email, "FILTER_VALIDATE_EMAIL")) {
$check = explode("#", $email);
if (in_array($check[1], $allowed_servers)) {
// server checks out
} else {
// server doesn't check out
}
} else {
// invalid email address
}
To begin with, requiring domain specific email addresses in a form means you need to utilize it at least server side (the mail addresses submitted needs to be checked againist non conforming user input). In simple words, if someone wants to "play" with your site he could send "bad" requests so that you store the wrong information in your database.
Moreover it's not compulsory but if you want to aid the user you can add client side verification.
As far as server side code is concerned you can use regular expressions to validate input. See here and here
Additionally, client side code can be used to assist the user in order to submit the form properly. You have more choices for this:
HTML5 regex
Javascript regex
in ProcessWire admin you're only able to log in using your name (username) but as I'm using e-mail log in in front end I want to use e-mail for backend, too.
How can I change admin login form to allow e-mail-address?
Here is the solution I came up with
I placed those hooks in my site/init.php file
// change login name input label to e-mail-address
$wire->addHookAfter('ProcessLogin::buildLoginForm', function(HookEvent $event) {
// on liner as we don't change anything else
$event->return->get('login_name')->set('label', $event->_('E-Mail-Address'));
});
// hook into session::login to get user by mail
$wire->addHookBefore('Session::login', function(HookEvent $event) {
// need to get email from $input as processLogin::execute is pageName sanitizing
$email = $event->input->post->email('login_name');
// stop here if login_name not a valid email
if (!$email) return;
// new selector arrays don't seem to work on $user so using $pages here
$user = $event->pages->get([['email', $email]]);
// if valid user set login name argument
if ($user->id) $event->setArgument('name', $user->name);
});
bare in mind that e-mail is not a unique field so if you don't ensure uniqueness of e-mail addresses this won't work, you could change it a little to overcome this though..
Have a look at https://processwire.com/talk/topic/1838-login-using-e-mail-rather-than-username-and-general-login-issues/ where Ryan posts some more infos about this and possible solutions in case of duplicate e-mail addresses
and https://processwire.com/talk/topic/1716-integrating-a-member-visitor-login-form/ for more on front-end login strategies
I have an array of emails. I would like to do some validation on each one to see if it is more or less the proper format. This script is validating emails for the administration side of a website. This particular piece of the administration side is for sending newsletters to particular groups, each containing an array of emails.
I am adding the functionality to add a group with administrator specified recipients. This would become useful if the administrator was getting rid of a particular group of newsletter recipients and wanted to add them to another group before destroying the original group.
I have come up with a way to throw an error if any of the items in the array do not match the validation, however, it seems like there should be a better way to do this in PHP. I have not been able to find an alternative method.
$email_array_data["count"] = count($email_array);
foreach($email_array as $email) {
if (email_validation_function($email) {
$email_array_data["passed_validation"]++;
} else {
$email_array_data["failed_validation"][] = $email;
}
}
if ($email_array_data["count"] == $email_array_data["passed_validation"]) {
Send The Emails
} else {
Echo The Emails That Failed Validation
}
This script works pretty well, but it seems like there would be a better way to do this that checking that every email met the requirements, then comparing the number of emails that passed/failed validation and the count of the emails array.
Is there a better method?
First, you should check out filter_var(). It's a great function for validating tons of data, especially emails (see here).
There are many ways to handle errors. Based on what I can see from your script, you are only considering the array valid if all emails are valid. You could throw an exception.
$is_valid = filter_var($email, FILTER_VALIDATE_EMAIL);
if( ! $email )
throw new Exception('Invalid email address supplied');
If you go this approach, you could catch the exception using a try {} catch {} Just another approach I guess.
I have used RS form form for user Registration on my joomla site. There i want to make registration from only company email address for internal users. How can i do this ? Please advice. Is there any way of configure custom email domain for RS Form email validation field?
You can use a Regular Expression as field validation (instead of email).
While on the RS Form, edit the field and go to Validations.
Change the Validation Rule to Regex.
Set the Regex Syntax to /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+#yourdomain.com$/
Note: be sure to change "yourdomain.com" to your own domain.
Validating emails using regular expressions is not perfect. See Using a regular expression to validate an email address. The first part of the regex I suggested here is taken from w3.
If you need a workaround and hardcode it, you can try the following approach in code:
// Domain to check
$email_domain = "#mydomain.com";
// Email to check
$co_email = "joefoobar#mydomain.com"; // returns true => valid
//$co_email = "joefoobar#gmail.com"; // returns false => invalid (uncomment to test)
if(!substr_count($co_email, $email_domain) > 0){
// Set the error
echo "Invalid!";
} else {
// No error, proceed with code
echo "Valid!";
}
I am a total php beginner. I actually want to customize a survey platform for visual stimuli for my needs as I need this for my thesis. The original version of this survey platform asks participants for their mail address to login then they see the available tests. As far as I understand it the code checks if the mail address is already saved in the database and if not reserves a certain number of the stimuli for the respective participant (with the use of their mail address as identifier). This number of stimuli will then be blocked from further tests. This however poses to problems:
1) participants can just participate once
2) participants have to give up their anonymity
The whole thing doesn't have to be very sophisticated. It would be sufficient if participants could log in with the word "anonymous" and then come to available tests where a certain number of stimuli is reserved not by their mail address but i.e. by an automatically produced unique random number which serves as an identifier.
I think the important lines of code are the following:
/**
* Lets user login. Stores new e-mail addresses if not yet in database.
*/
function login() {
$this->validation->set_error_delimiters('<div class="error">', '</div>');
$this->validation->set_message('required', $this->lang->line("validation_error_validemail_required"));
$this->validation->set_message('valid_email', $this->lang->line("validation_error_validemail_required"));
// check if valid e-mail address
//required|valid_email
$rules['email'] = "required|valid_email";
$this->validation->set_rules($rules);
$fields['email'] = $this->lang->line("frontend_user_email");
$this->validation->set_fields($fields);
if ($this->validation->run() == FALSE) {
$this->load->view('welcome_message'); // reload form
} else {
$this->load->model("testcontainer_model", 'testcontainer');
$this->data['testcontainer']->activeuser = $this->data['testcontainer']->getUserByEmail($_POST['email']);
$this->load->view('welcome_message', $this->data);
}
}
/**
* Logout user, unset session data.
*/
function logout() {
$this->session->unset_userdata('user_email');
$this->session->unset_userdata('user_id');
$this->session->unset_userdata('albumpath_relative');
$this->session->unset_userdata('test_name');
$this->session->unset_userdata('test_id');
$this->load->view('welcome_message');
}
Would you please be so nice to help me modifying this code to get what I want want? I am grateful for every help!
Best regards,
Andreas
Forms in CI can be complex. This can be a very helpful tool if you wan to save some time too:
http://formigniter.org/app/index.php/formigniter/index/5