Help capitalizing a direction in a string

4 views
Skip to first unread message

CroNiX

unread,
Mar 12, 2009, 2:34:17 PM3/12/09
to MooTools Users
This one is above my head and I could use a little help. I am writing
a blur event for a form field that will take a string, in this case an
address, and search for the "direction" in the address and capitalize
it.

Example: "123 n first street" converts to "123 N first street"
Example: "123 NW third street" converts to "123 NW third street"

I believe the string.replace method could be used but don't know how
to write the regex.
Heres what I have:
$$('.addr').each(function(el){
el.addEvent('blur', function(){
var value = el.get('value');
var directions = [' n ', ' s ', ' e ', ' w ', ' ne ', ' nw ', ' se
', ' sw '];
var replacements = {' n ': ' N ', ' s ': ' S ', ' e ': ' E ', ' w ':
' W ', ' ne ': ' NE ', ' nw ': ' NW ', ' se ': ' SE ', ' sw ': ' SW '}
//var regex = ???;

directions.each(function(direction){
if(value.contains(direction)){
console.log('string contains the direction: ' + direction);
//value.replace(replacements, regex);
}
});
});
});
Its not much, all I have done so far is detect whether or not the
string contains the direction.
Any help is much appreciated.

CroNiX

unread,
Mar 12, 2009, 2:35:54 PM3/12/09
to MooTools Users
oops, second example should be:
Example: "123 nw third street" converts to "123 NW third street"

CroNiX

unread,
Mar 12, 2009, 3:24:11 PM3/12/09
to MooTools Users
I solved it with this:
$$('.addr').each(function(el){
el.addEvent('blur', function(){
var value = el.get('value');
var directions = [' n ', ' s ', ' e ', ' w ', ' ne ', ' nw ', ' se
', ' sw '];

directions.each(function(direction){
if(value.contains(direction)){
var newstr = value.replace(direction, direction.toUpperCase(),
"gi");
el.set('value', newstr.capitalize());
}
});
});
});

"123 sw third street" now becomes "123 SW Third Street"

If someone has a better solution, I'd like to know :)

Fábio Costa

unread,
Mar 12, 2009, 4:55:31 PM3/12/09
to mootool...@googlegroups.com
replace(/\s+(n|s|e|w|ne|nw|se|sw)\s+/, function(match){
            return match.toUpperCase();
        });

should work...

Fábio Miranda Costa
Engenheiro de Computação
http://meiocodigo.com

CroNiX

unread,
Mar 12, 2009, 4:59:28 PM3/12/09
to MooTools Users
Hey Fabio,
Thanks. Will that take into account the space before and after the
"direction"?

On Mar 12, 1:55 pm, Fábio Costa <fabiomco...@gmail.com> wrote:
> replace(/\s+(n|s|e|w|ne|nw|se|sw)\s+/, function(match){
>             return match.toUpperCase();
>         });
>
> should work...
>
> Fábio Miranda Costa
> Engenheiro de Computaçãohttp://meiocodigo.com

CroNiX

unread,
Mar 12, 2009, 5:07:04 PM3/12/09
to MooTools Users
Just tested and works great. Thank you Fabio. Heres the code for my
function:
$$('.addr').each(function(el){
el.addEvent('blur', function(){
var value = el.get('value');
value = value.replace(/\s+(ne|nw|se|sw)\s+/, function(match){
return match.toUpperCase();
});
el.set('value', value.capitalize());
});
});

I was able to get rid of the 'n', 's', 'e', 'w' as the
value.capitalize takes care of those.
Awesome, thanks again!

Fábio Costa

unread,
Mar 12, 2009, 5:39:40 PM3/12/09
to mootool...@googlegroups.com
hmm nice to help you.



Fábio Miranda Costa
Engenheiro de Computação
http://meiocodigo.com


ibolmo

unread,
Mar 12, 2009, 6:46:33 PM3/12/09
to MooTools Users
hrm no need for each(function(el)...

you can just do $$('.addr').addEvent(...

but you'll need to replace el with this.

Olmo

Guillermo Rauch

unread,
Mar 12, 2009, 10:54:54 PM3/12/09
to mootool...@googlegroups.com
And don't create a function in the event, create it beforehand and reuse it within the event, to optimize.
--
Guillermo Rauch
http://devthought.com

CroNiX

unread,
Mar 12, 2009, 11:29:26 PM3/12/09
to MooTools Users
Even if its only being used on 1 field on 1 page?

Guillermo Rauch

unread,
Mar 12, 2009, 11:47:01 PM3/12/09
to mootool...@googlegroups.com
It's still good practice and future-proof imho
Reply all
Reply to author
Forward
0 new messages