I need to properly format a Canadian zip code if its entered in wrong.
Format is ### ### where "#" can be either a number or letter for example : M5R 2G3
I've tried this: (its broken up for testing purposes)
shipping.zip = shipping.zip.toUpperCase().split('')
shipping.zip = shipping.zip.splice(3, 0, ' ')
shipping.zip = shipping.zip.join().replace(/,/g, '');
But when I enter in :
m5r2g3
I Get this:
[ 'M', '5', 'R', '2', 'G', '3' ]
[ ]
And thats it. I have no idea why its not working. Please help. Thanks.
I need to properly format a Canadian zip code if its entered in wrong.
Format is ### ### where "#" can be either a number or letter for example : M5R 2G3
I've tried this: (its broken up for testing purposes)
shipping.zip = shipping.zip.toUpperCase().split('')
shipping.zip = shipping.zip.splice(3, 0, ' ')
shipping.zip = shipping.zip.join().replace(/,/g, '');
But when I enter in :
m5r2g3
I Get this:
[ 'M', '5', 'R', '2', 'G', '3' ]
[ ]
And thats it. I have no idea why its not working. Please help. Thanks.
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Feb 24, 2014 at 22:21 JDillon522JDillon522 19.7k15 gold badges50 silver badges82 bronze badges1 Answer
Reset to default 9'm5r2g3'.toUpperCase().replace(/\W/g,'').replace(/(...)/,'$1 ') // "M5R 2G3"
The replace(/\W/g,'')
removes all non-alphanumeric characters (including mas).