1

Here's the Ajax code:

jQuery.ajax({url: "http://api.tumblr.com/v2/blog/" + nonTumblrURL + "/info?api_key=WIHfFzkpSXJ4FOfzt3qBAhlleM09iSAtc2AFNAdViAeRPzAsMq&jsonp=getUserName", dataType: "jsonp"});

Here's the function:

function getUserName(data){
   console.log(data.response.blog.name);
   var username = data.response.blog.name;

if (username === "api" ){
    //alert("You didn't enter anything.");
    $('input.nonTumblrURL').val("You need to enter something here.");
    return;}
$('#convertedURL').html("<em>Use </em><b> " + username + "</b> <em> for that Tumblr</em>");
}

The purpose here is to retrieve a Tumblr's username when a custom domain like "customdomain.com" is used instead of the standard "someone.tumblr.com". So, I use the Tumblr API to get the username.

When a blank value is sent in the Ajax call, it returns "api" so I trapped that in the function and placed a results message in #convertedURL and works fine.

What I need is something that will trap a non-blank value that won't resolve in the Tumblr API. For example, "theagenda.nytimes.com" properly resolves to "nyt-agenda".

But, I need to trap something like "junkkkkk" (and others which are not a valid domains for Tumblr or otherwise) and replace the HTML in #convertedURL with a message like "That's not a custom domain in use".

Thanks in advance for your help!

Robin Maben
  • 22,194
  • 16
  • 64
  • 99
user1452893
  • 838
  • 1
  • 11
  • 29

1 Answers1

1
function isValidDomain(url){
  return /((http|https):\/\/(\w+:{0,1}\w*@)?(\S+)|)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/.test(url);
} 

Blatantly copied from here. Also see this.

Second, to know whether it resolved to a valid url. (Not speaking tumblr-specific here)

You could simply make the ajax call and see whether it succeeded or failed.

$.ajax({url : url,
   success : function() {...},
   error : function() {...}
});
Community
  • 1
  • 1
Robin Maben
  • 22,194
  • 16
  • 64
  • 99
  • Success and error return the same: "nay" even when it IS successful: `$.ajax({url : "http://api.tumblr.com/v2/blog/" + nonTumblrURL + "/info?api_key=WIHfFzkpSXJ4FOfzt3qBAhlleM09iSAtc2AFNAdViAeRPzAsMq&jsonp=getUserName", dataType: "jsonp", success : function() {console.log("yay")}, error : function() {console.log("nay")} });` – user1452893 Aug 06 '12 at 23:11