jmerrick0657 Posted July 12, 2019 Share Posted July 12, 2019 I'm trying to force formatting on an email address. What my customer wants is First.Last@CassiaLife.org I sort of got this to work, so if I enter first.last or first.last@whatever.com it will return the correct result: var str = Field("email"); var firstletter = Left(Field("email"),1); firstletter = ToUpper(firstletter); var letterposition = Field("email").indexOf(".")+1 var letter = Mid(Field("email"),letterposition+1,1); letter = ToUpper(letter); var front = str.substr(1,str.indexOf(".")); var length = Field("Email").length - letterposition; var back = Mid(Field("Email"),letterposition+2,length-1); var fixed = firstletter + front + letter + back; var str = fixed; var newstr = str.substr(0,str.indexOf("@")); if (Field("Email") != '') { if (newstr != "") return newstr + "@CassiaLife.org"; else return fixed + "@CassiaLife.org"; } else return '' However I'm running into two issues: 1. The code seems kind of clunky (I'm not a Javascript expert by any means) 2. It only works on the character after the first period. While I doubt that anyone would have more than one period, it would be nice if I could figure out a way to account for that possibility. Quote Link to comment Share on other sites More sharing options...
Dan Korn Posted July 12, 2019 Share Posted July 12, 2019 It's not completely clear to me what all the requirements are, in terms of every possible data field and what the expected result is for each. But if all you're trying to is take whatever is in the data field, strip off any existing email domain (anything including and after the @ sign), then append your own domain at the end, then I think this will work: return Field("email").replace(/@.*/, '') + "@CassiaLife.org"; Quote Link to comment Share on other sites More sharing options...
ScottHillock Posted July 12, 2019 Share Posted July 12, 2019 I would suggest this: var email = 'first.middle.last@whatever.com'; if (!email){return "";} //remove all text after @ symbol, then split into an array based on periods names = email.replace(/@.*/, '').split('.'); //replace first letter with upper, and concat with the rest of the string for (i = 0; i < names.length; i++){ names[i] = ToUpper(names[i][0]) + names[i].substring(1); } //join back names with a period separator and add domain return names.join('.') + "@CassiaLife.org"; You could also do the following: var email = 'first.middle.last@whatever.com'; if (!email){return "";} return email.replace(/@.*/, '').replace(/(^|[.]+)([a-z])/g, function (m, $1, $2) { return $1 + $2.toUpperCase(); }) + "@CassiaLife.org"; But, it's harder to adjust in case you wanted to make the names proper case, or remove spaces, etc. Quote Link to comment Share on other sites More sharing options...
jmerrick0657 Posted July 22, 2019 Author Share Posted July 22, 2019 Thanks Scott! That was exactly what I was trying to do. 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.