MyBB Community Forums

Full Version: Remove blank spaces from Email in registration form
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Looks like my average guests are quite noob and many of them keep adding a space at the end of their email address inside either Email field or Email Confirmation field, inside User Registration page.

When registration in submitted, MyBB prints an error claiming that emails are different, and my users give up with registration, which turns in losing forum members.

How can I add a code that checks if there are white spaces and removes them before comparing the two fields?
Hi,

make a backup of the file "member.php".

Now, in that file,  search for:
$email = htmlspecialchars_uni($mybb->get_input('email'));
$email2 = htmlspecialchars_uni($mybb->get_input('email2'));

(Lines 310 and 311)

And change it to:
$email = htmlspecialchars_uni(trim($mybb->get_input('email')));
$email2 = htmlspecialchars_uni(trim($mybb->get_input('email2')));


Note 1: Remeber to use a program like Notepad++ or similar to edit this file and save it without BOM.

Note 2: whenever you update your MyBB forum, if the "member.php" is updated, you'll have to change it again.
(2019-09-10, 01:09 PM)NoRules Wrote: [ -> ]Hi,

make a backup of the file "member.php".

Now, in that file,  search for:
$email = htmlspecialchars_uni($mybb->get_input('email'));
$email2 = htmlspecialchars_uni($mybb->get_input('email2'));

(Lines 310 and 311)

And change it to:
$email = htmlspecialchars_uni(trim($mybb->get_input('email')));
$email2 = htmlspecialchars_uni(trim($mybb->get_input('email2')));


Note 1: Remeber to use a program like Notepad++ or similar to edit this file and save it without BOM.

Note 2: whenever you update your MyBB forum, if the "member.php" is updated, you'll have to change it again.

I did it, updated member.php, but the error keeps repeating.

I'm using these emails as test to register a new account:

"[email protected] "

"test@k .com"
(2019-09-10, 01:09 PM)NoRules Wrote: [ -> ]
$email = htmlspecialchars_uni($mybb->get_input('email'));
$email2 = htmlspecialchars_uni($mybb->get_input('email2'));

(Lines 310 and 311)

No, not these lines. They're for maintaining inputted values while displaying errors in a consecutive page, except passwords of course. Should do something before the data being sent to the datahandler, say before line 203:
	$userhandler->set_data($user);


(2019-09-10, 01:19 PM)ShadowOne Wrote: [ -> ]
"test@k .com"

This one is indeed an invalid email address.

I was assuming you're having this kind of email input:

"  [email protected]  "
Ok,

I thought it was only at the begining and in the end.

Change it for this:
$email = htmlspecialchars_uni($mybb->get_input('email'));
$email = preg_replace('/\s+/', '', $email);
$email2 = htmlspecialchars_uni($mybb->get_input('email2'));
$email2 = preg_replace('/\s+/', '', $email2);
(2019-09-10, 01:22 PM)noyle Wrote: [ -> ]
(2019-09-10, 01:09 PM)NoRules Wrote: [ -> ]
$email = htmlspecialchars_uni($mybb->get_input('email'));
$email2 = htmlspecialchars_uni($mybb->get_input('email2'));

(Lines 310 and 311)

No, not these lines. They're for maintaining inputted values while displaying errors in a consecutive page, except passwords of course. Should do something before the data being sent to the datahandler, say before line 203:
 $userhandler->set_data($user);


(2019-09-10, 01:19 PM)ShadowOne Wrote: [ -> ]
"test@k .com"

This one is indeed an invalid email address.

I was assuming you're having this kind of email input:

[email protected]  "

I'm convinced that most of my users are adding an extra space at end of their email

But considering that blank spaces are never valid inside an email, and that some smartphones have a keyboard with ".com" shortcut that may automatically add a blank space before adding it, I also took the case where blank space may be in the middle of email too.

(2019-09-10, 01:28 PM)NoRules Wrote: [ -> ]Ok,

I thought it was only at the begining and in the end.

Change it for this:
$email = htmlspecialchars_uni($mybb->get_input('email'));
$email = preg_replace('/\s+/', '', $email);
$email2 = htmlspecialchars_uni($mybb->get_input('email2'));
$email2 = preg_replace('/\s+/', '', $email2);

This one removed blank spaces, but only after sending the registration form and printing the error.

Users should hit the "submit" button again to register.

I'm looking for the point where registration is sent, to add the same code when registration is sent the first time.
(2019-09-10, 01:22 PM)noyle Wrote: [ -> ]No, not these lines. They're for maintaining inputted values while displaying errors in a consecutive page, except passwords of course. Should do something before the data being sent to the datahandler, say before line 203:
 $userhandler->set_data($user);

Yeah, right, need coffee....

Change 
"email" => $mybb->get_input('email'),
"email2" => $mybb->get_input('email2'),
Lines 167 and 168

to:
"email" => preg_replace('/\s+/', '', $mybb->get_input('email')),
"email2" => preg_replace('/\s+/', '', $mybb->get_input('email2')),
(2019-09-10, 01:34 PM)NoRules Wrote: [ -> ]
(2019-09-10, 01:22 PM)noyle Wrote: [ -> ]No, not these lines. They're for maintaining inputted values while displaying errors in a consecutive page, except passwords of course. Should do something before the data being sent to the datahandler, say before line 203:
 $userhandler->set_data($user);

Yeah, right, need coffee....

Change 
"email" => $mybb->get_input('email'),
"email2" => $mybb->get_input('email2'),
Lines 167 and 168

to:
"email" => preg_replace('/\s+/', '', $mybb->get_input('email')),
"email2" => preg_replace('/\s+/', '', $mybb->get_input('email2')),

Solved! Thank you!!
In a current version of MyBB, the registration form will validate the email input at runtime. Your issue won't happen then.
Which version or theme are you running?

Without editing the hard PHP code, you can add an additional email validation at runtime to the registration form with JavaScript onfocusout():
<input ... email ... onfocusout="validateMail()">
<input ... email2 ... onfocusout="compareMail()">

When the address is typed in and the user switches to the next input field a simple JavaScript function validates the first input... and checks the second one as well. A wrong input may change the text-color to red or change the CSS class to input.error for example. The user alerted in this moment and not after submitting the form.

A piece of JS code can be put anywhere within the template.
Search for any simple JS email validation snippet and you'll find a bunch.

[ExiTuS]
(2019-09-10, 01:53 PM)[ExiTuS] Wrote: [ -> ]In a current version of MyBB, the registration form will validate the email input at runtime. Your issue won't happen then.
Which version or theme are you running?

Without editing the hard PHP code, you can add an additional email validation at runtime to the registration form with JavaScript onfocusout():
<input ... email ... onfocusout="validateMail()">
<input ... email2 ... onfocusout="compareMail()">

When the address is typed in and the user switches to the next input field a simple JavaScript function validates the first input... and checks the second one as well. A wrong input may change the text-color to red or change the CSS class to input.error for example. The user alerted in this moment and not after submitting the form.

A piece of JS code can be put anywhere within the template.
Search for any simple JS email validation snippet and you'll find a bunch.

[ExiTuS]

Are you sure?
I'm on 1.8.21 (just upgraded) and using default theme with few style edits (just colors)
Pages: 1 2