Stack Posted April 17, 2015 Share Posted April 17, 2015 I'm stuck editing a rule that will delete the "@" + the domain from an Email variable. In other words, if the email is sample@example.com, I'd like "@example.com" to be removed. I adapted the code below from this post: var title = Field("Email"); // determine position of colon in string var colonLoc = title.indexOf("@"); // remove chars to left of colon plus colon itself title = Right(title, title.length-colonLoc-1); // remove any leading or trailing spaces title = Trim(title); return title; The code almost works, except it does the complete opposite. It keeps "@example.com" and removes "sample". I don't have a full grasp of JavaScript however, in my mind, on the 5th line of code where it says "Right(title, ..." I should just change to "Left(title, ...". That unfortunately does not work. Can anybody lend a hand? Quote Link to comment Share on other sites More sharing options...
David Miller Posted April 17, 2015 Share Posted April 17, 2015 (edited) You can try this: return ToLower(Field("Email")).split("@",1) It returns everything to the left of the at symbol (a.k.a., commercial at, oancea, strudel) in lower case. Found it in the forums awhile ago. Edited April 18, 2015 by David Miller Quote Link to comment Share on other sites More sharing options...
Stack Posted April 17, 2015 Author Share Posted April 17, 2015 Perfect, thank you!!! Quote Link to comment Share on other sites More sharing options...
step Posted April 20, 2015 Share Posted April 20, 2015 You can try this: return ToLower(Field("Email")).split("@",1) It returns everything to the left of the at symbol (a.k.a., commercial at, oancea, strudel) in lower case. Found it in the forums awhile ago. This will more than likely work for this particular use case, however I just want to point out that using the split function creates an array from the string. Specifying '1' as the second parameter limits the length of the array to 1 but the result is still an array. The only reason I bring this up is because you will run into issues later if you try to perform String specific methods to that resulting variable. For example, let's say my email address is "ste.pennell@email.com". I can split that string at the "@" to get my first and last name: var email = 'ste.pennell@email.com'; var name = email.split('@',1); return name; // ste.pennell But if I wanted to split the 'name' variable on the 'period' to get just the first name, it will throw an error because 'split' works on strings – not arrays: var email = 'ste.pennell@email.com'; var name = email.split('@',1); // ste.pennell var firstName = name.split('.',1); return firstName; // type error: firstName.split is not a function You can make a minor modification to your original code and specify the position in the array you'd like to return to return the actual string like so: var email = 'ste.pennell@email.com'; var name = email.split('@',1)[color="Red"][0][/color]; // ste.pennell var firstName = name.split('.')[color="red"][0][/color]; // ste var lastName = name.split('.')[color="red"][1][/color]; // pennell return firstName; // ste But honestly for this scenario, since you have no intention of using the '@' or anything after it, I would just replace it all together: var email = 'ste.pennell@email.com'; var name = email.replace(/@.*$/,''); // ste.pennell var firstName = name.split('.')[0]; return firstName; // ste Or specifically to your case: return Field("Email").replace(/@.*$/,''); Quote Link to comment Share on other sites More sharing options...
Stack Posted April 20, 2015 Author Share Posted April 20, 2015 This will more than likely work for this particular use case, however I just want to point out that using the split function creates an array from the string. Specifying '1' as the second parameter limits the length of the array to 1 but the result is still an array. The only reason I bring this up is because you will run into issues later if you try to perform String specific methods to that resulting variable. For example, let's say my email address is "ste.pennell@email.com". I can split that string at the "@" to get my first and last name: var email = 'ste.pennell@email.com'; var name = email.split('@',1); return name; // ste.pennell Thanks for the heads up on this. We don't typically see email addresses with the FirstName.LastName format, but it's good to be safe and I know I'd be scrambling for an answer the first time we do see an email in this format. Quote Link to comment Share on other sites More sharing options...
David Miller Posted April 21, 2015 Share Posted April 21, 2015 (edited) Very interesting Step. Seriously, learned a lot. Posts like this by you, Dan and a few others are much appreciated (REALLY!!!). The explanations are extremely helpful. This is how I (we) learn. Many of you are way more qualified in JavaScript than I am. The JavaScript Rule I posted here appears to be somewhat limited. But, it seems to work well for my FusionPro Templates. I've used it sucessfully for several years. That said, isn't it possible to convert a JavaScript Array into a String? Using the .toString() Method? Edited April 21, 2015 by David Miller Quote Link to comment Share on other sites More sharing options...
step Posted April 21, 2015 Share Posted April 21, 2015 Very interesting Step. Seriously, learned a lot. Posts like this by you, Dan and a few others are much appreciated (REALLY!!!). The explanations are extremely helpful. This is how I (we) learn. That is exactly why I decided to post. I've learned a lot through other people doing the same thing. I was really hoping that I didn't come off condescending by replying when you had already given a working solution That said, isn't it possible to convert a JavaScript Array into a String? Using the .toString() Method? Yeah, there are certainly ways to convert an array to a string and using the toString method would work but you want to be careful not to use it on the entire array if the length is greater than 1. Essentially what you're doing when you split a string is creating an array at the split character. So, splitting the email address at the @ would create an array with a length of 2 where position 0 is the name (everything before the @) and position 1 is the domain (everything after the @): var email = "ste.pennell@email.com"; email = email.split("@"); // <-- basically creates this array: ["ste.pennell", "email.com"] return email; // <-- returns "ste.pennell,email.com" //return email.toString(); // <-- returns "ste.pennell,email.com" You can access the strings at each of those positions by specifying the position: return email[0]; // "ste.pennell" //return email[1]; // "email.com" Since you specified '1' as the second paramater in the split, you're basically saying "only split the string once" (or "the resulting array should have a length of 1"). So it still creates an array but since the length is only 1 position, it doesn't look weird when you return it because there's no comma separating the second position from the first: var email = "ste.pennell@email.com"; email = email.split("@",1); // <-- basically creates this array: ["ste.pennell"] return email; // <-- returns "ste.pennell" It looks like it's a string but it's still an array and here's how you can tell: return typeof email; // object While specifying the position, gives you the actual string that you split: return typeof email[0]; // string Arrays can be very helpful but it sometimes seems easier to use string specific methods like "replace" in order to keep the string a string and avoid muddying up the code with converting the array back to a string. Though, as long as you understand what's happening to the string when you use a "split" then it shouldn't be a problem to figure out how to access the string again if you need to. Quote Link to comment Share on other sites More sharing options...
Dan Korn Posted April 21, 2015 Share Posted April 21, 2015 For what it's worth, I would also prefer this solution: return Field("Email").replace(/@.*$/,''); This uses Regular Expressions, and ensures that what gets returned is a String. It's actually a lot simpler than dealing with an Array. And it's more easily modified to do more advanced kinds of input validation. Quote Link to comment Share on other sites More sharing options...
David Miller Posted April 23, 2015 Share Posted April 23, 2015 (edited) That is exactly why I decided to post. I've learned a lot through other people doing the same thing. I was really hoping that I didn't come off condescending by replying when you had already given a working solution No Step. You didn't come off as being condescending. If there is a better way of doing something, I want to know. Others in this forum should know too. (Besides, I've got tougher skin than that!) For what it's worth, I would also prefer this solution: return Field("Email").replace(/@.*$/,''); Thanks for the confirmation Dan. Will certainly use this solution going forward. That said, I use this forum on a daily basis. Most of the solutions I need are already here. Rarely need to ask. Thanks to you both. Edited April 23, 2015 by David Miller Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.