Jump to content

Format Text Like a Phone number


Sellis

Recommended Posts

So I have an interesting issue.

 

My client needs to have text formatted like a phone number.

 

A little background this client is using a braille translating service to convert a phone number to Grade 2 braille.

 

so 617-555-1234 is represented like this. #fag-eee-abcd

 

Here is what I need help doing. The text must always have (#) in the front to note that this is a number, and we must be bale to have the rule format the text like a number #fag-eee-abcd.

 

We want to set the programming to be bale to accommodate the text entry of #fag-eee-abcd or #fageeeabcd.

 

I tried modifying the format phone rule, but with little success, any one have any suggestions for this?

 

Thank you

Sean

Link to comment
Share on other sites

You could use a function like this one:

function formatBraille(number) {
 // Remove any characters that aren't a-j
 // and consider the number a match if it has 7 or 10 letters in it
 number = number.replace(/[^a-j]/gi, '').match(/^(.{3})?(.{3})(.{4})$/);

 // If no match is found, return an empty string
 if (!number) return '';

 // Remove the original string, leaving only the
 // captured groups in the 'matched' array
 number.shift();

 // Remove any values that are 'undefined' and join with a hyphen
 return '#' + number.filter(Boolean).join('-');
}

 

You can put that in your JavaScript Globals so that you can call it from any rule, or you can just put it in the same rule and call it like this:

return formatBraille('fageeeabcd');    // #fag-eee-abcd

 

If you wanted to take it a step further, you could have the function convert regular phone numbers into the braille format:

function formatBraille(number) {
 number = number
   [color="Red"].split('')
   .map(function(s) { return /\d/.test(s) ? Chr(96 + Int(s)) : s; })
   .join('')[/color]
   .replace(/[^a-j]/gi, '')
   .match(/^(.{3})?(.{3})(.{4})$/);
 if (!number) return '';
 number.shift();
 return '#' + number.filter(Boolean).join('-');
}

Then you could pass either format and get the braille format back:

return formatBraille('(617) 555-1234'); // #fag-eee-abcd

Link to comment
Share on other sites

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...