Jump to content

Replacing character / formatting a string


Recommended Posts

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.

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 2 weeks later...

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...