SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to strip the 'name@' from email address

    I need to extract just the domain from an entered email address. So for example, if a user entered 'myname@somedomain.com' I would like to return 'somedomain.com'

    I'm a bit of a JS newb, and can't quite get a grip on the regular expressions thing... can someone help me out please?

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var str = 'myname@somedomain.com';
    str = str.replace(/^.+@/, '');
    ^ means "this pattern starts at the beginning of the string in question"
    .+ means "one or more of any character"
    @ means "@" of course.

    this means the pattern will match things like david@@@@@smith@domain.com. You can make the regular expression slightly tighter by allowing only certain characters before the "@", but you run the risk of someone with an odd address with "funny" characters in it not matching the pattern. Also, I don't really know what characters are allowed before an "@".

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,608
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Code:
    function getEmailDomain(address) {
    var x = address.split('@');
    if (x.length > 1) return x[1];
    else return 'no @ found';
    }
    Then simply reference getEmailDomain('myname@somedomain.com') in your code where you want the domain name part to be used.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks!

    Thanks for your responses.

    I found a great tutorial here: http://www.webmonkey.com/webmonkey/9...tw=programming

    and ended up with:
    Code:
    var setURLfromEmail = function() {
    	var addy = $('email').getValue();
    	var urlexists = $('companyurl').getValue();
    	if (!urlexists || urlexists.trim()==''){
    		//strip name and @ from email address
    		var at = addy.indexOf("@");
    		var domain_start = at + 1;
    		var domain = addy.substring(domain_start, addy.length);
    		domain = "www."+domain;
    		$('companyurl').setProperty('value', domain);
    	}
    };
    (I'm using mootools to access the form elements)

    So this will take an email whoever@somedomain.com and if the 'companyurl' input box is empty, it will set it's value to 'www.somedomain.com'

    Thanks again for your responses.
    -chad


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •