...

View Full Version : add domain on onBlur of email form



glennt
12-25-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: frank@email.com) 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!

zfs
12-25-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;
}
</script>

<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!

glennt
12-26-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.

Arbitrator
12-26-2006, 01:37 AM
You can probably do that by changing the line


if (spl.length == 1)

to


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

Ancora
12-26-2006, 01:38 PM
glennt:



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

glennt
12-29-2006, 06:59 PM
Sweet! thanks guys



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum