1

I have a button that finds the website if it's in Salesforce or if blank, uses the email domain or uses the Company name. Someone decided to enter "http://No website (Date)" in a host of Website fields which means the code I'm using thinks this is the website and goes no further. I need to include in my code something that says If Website Begins with "http://No website" check the email.

The code I'm using is:

var website = '{!JSENCODE( Customer_Support_Survey__c.Company_Website__c )}';
var email = '{!Customer_Support_Survey__c.Customer_Contact_Email__c}';
var company = '{!JSENCODE( Customer_Support_Survey__c.Company_Name__c )}';
var targetURL = '';

// IF WEBSITE FIELD IS POPULATED WE GO THERE, NATURALLY

if(website != ''){

// CHECK IF WE NEED TO ADD http:// OR NOT
if(website.indexOf('//') != -1)
targetURL = website;
else
targetURL = 'http://' + website;
}

// NEXT WE CHECK OUT THE DOMAIN OF THE EMAIL ADDRESS

else if(email != ''){

var domain = email.substring(email.indexOf('@') + 1,email.length);

// WE FILTER OUT COMMON EMAIL DOMAINS
if(domain!='gmail.com' && domain!='yahoo.com' && domain!='hotmail.com' && domain!='me.com' && domain!='excite.com' && domain!='aol.com')
targetURL = 'http://' + domain;
}

// IF NO TARGET FOUND YET WE TAKE A STAB USING COMPANY

if(targetURL==''){

targetURL = 'http://';

// FAIR ASSUMPTION IS TO SWAP '&' for 'and'
company = company.replace(/\&/g,'and');

// STRIP OUT INVALID DOMAIN CHARACTERS
targetURL += company.replace(/[^a-zA-Z0-9|\-]/g,'') + '.com.au';

}

// FINALLY WE OPEN A NEW WINDOW DIRECTED TO OUR TARGET URL

window.open(targetURL,'_blank','',false);
0

As per OP you just need to check if your website field start with "http://No website".

For this, You can use startwith function to check if variable website start with above string

var str = "http://No website sdfsdfsdfsdf ";
var n = str.startsWith("http://No website");
alert(n);

If you intend to validate the compete website value "http://No website" string then you will have to use generic regular expression something like below.

var re = /^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/;
if (!re.test(url)) { 
    alert("url error");
    return false;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.