View Full Version : add domain on onBlur of email form

Dec 25th, 2006, 09:07 PM
Hi. Does anyone know how to automatically add the domain name at the end of an email address field? What im looking for is a script that checks the email address a user enters and if they type their full email (ex: [email protected]) it would just do nothing, but if they only enter their username (ex: frank), it would automatically add the "@email.com" part when they click out of the email field. I've tried modifying a few other scripts i found online but I can't seem to make it work. If anyone knows how to do this I would greatly appreciate some help. If the description needs to be clearer please let me know. thanks!

Dec 25th, 2006, 10:33 PM
That should do it:

<script type="text/javascript">
function append_domain(domain) {
var f = document.getElementById('email');
var spl = f.value.split('@');
if (spl.length == 1)
f.value += '@' + domain;

<input id="email" name="email" value="" onblur="append_domain('email.com');">

I assumed that you always want to append the same domain name (email.com) if it is not specified.

PS. Don't forget to set id (marked red) to your input field!

Dec 26th, 2006, 12:27 AM
WORKS!! thanks 100000000000%. Very fast reply too.

I know i didnt mention this in the original post (just thought of it) but is there a way to make it so that if the user focuses on the email field but doesn't type anything it will stay blank when they click out of it? Thanks again.

Dec 26th, 2006, 01:37 AM
You can probably do that by changing the line

if (spl.length == 1)


if (f.value && spl.length == 1)

Dec 26th, 2006, 01:38 PM

<label>Email: <input type='text' name='email' size='15' onblur="/\@|^.{0,0}$/g.test(this.value) ? '' : this.value +='@email.com'"></label>

Dec 29th, 2006, 06:59 PM
Sweet! thanks guys