Standardize phone numbers in submitted form with ColdFusion












1















We're looking for a way to standardize the entry of phone numbers entered in a form to be, for example, (444) 555-666. Right now you can enter it as any format, such as 4445556666 or 444-555-6666 and there's no standardization. I'd like a way for the number to be converted to the former format when it's entered into the database.



Here is the HTML code for the form:



<tr>
<td align="right">
<label for="phone_number">Telephone:</label>
</td>
<td>
<input type="text" name="phone_number" value="#form.phone_number#" />
</td>
</tr>


This, along with all other data, is entered into the database using a cfquery INSERT INTO command. I can provide that code if needed. Here's an abbridged version:



INSERT INTO Schedule_Registrations(
phone_number
)
VALUES(
'#FORM.phone_number#'
)


Any thoughts on this would be appreciated.










share|improve this question

























  • You should really be using cfqueryparam. Never insert user-accessible data directly into a query. You are risking a sql injection attack. Replace '#form.phone_number#' with <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.phone_number#" />

    – Sean Coyne
    Dec 28 '11 at 0:01
















1















We're looking for a way to standardize the entry of phone numbers entered in a form to be, for example, (444) 555-666. Right now you can enter it as any format, such as 4445556666 or 444-555-6666 and there's no standardization. I'd like a way for the number to be converted to the former format when it's entered into the database.



Here is the HTML code for the form:



<tr>
<td align="right">
<label for="phone_number">Telephone:</label>
</td>
<td>
<input type="text" name="phone_number" value="#form.phone_number#" />
</td>
</tr>


This, along with all other data, is entered into the database using a cfquery INSERT INTO command. I can provide that code if needed. Here's an abbridged version:



INSERT INTO Schedule_Registrations(
phone_number
)
VALUES(
'#FORM.phone_number#'
)


Any thoughts on this would be appreciated.










share|improve this question

























  • You should really be using cfqueryparam. Never insert user-accessible data directly into a query. You are risking a sql injection attack. Replace '#form.phone_number#' with <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.phone_number#" />

    – Sean Coyne
    Dec 28 '11 at 0:01














1












1








1


1






We're looking for a way to standardize the entry of phone numbers entered in a form to be, for example, (444) 555-666. Right now you can enter it as any format, such as 4445556666 or 444-555-6666 and there's no standardization. I'd like a way for the number to be converted to the former format when it's entered into the database.



Here is the HTML code for the form:



<tr>
<td align="right">
<label for="phone_number">Telephone:</label>
</td>
<td>
<input type="text" name="phone_number" value="#form.phone_number#" />
</td>
</tr>


This, along with all other data, is entered into the database using a cfquery INSERT INTO command. I can provide that code if needed. Here's an abbridged version:



INSERT INTO Schedule_Registrations(
phone_number
)
VALUES(
'#FORM.phone_number#'
)


Any thoughts on this would be appreciated.










share|improve this question
















We're looking for a way to standardize the entry of phone numbers entered in a form to be, for example, (444) 555-666. Right now you can enter it as any format, such as 4445556666 or 444-555-6666 and there's no standardization. I'd like a way for the number to be converted to the former format when it's entered into the database.



Here is the HTML code for the form:



<tr>
<td align="right">
<label for="phone_number">Telephone:</label>
</td>
<td>
<input type="text" name="phone_number" value="#form.phone_number#" />
</td>
</tr>


This, along with all other data, is entered into the database using a cfquery INSERT INTO command. I can provide that code if needed. Here's an abbridged version:



INSERT INTO Schedule_Registrations(
phone_number
)
VALUES(
'#FORM.phone_number#'
)


Any thoughts on this would be appreciated.







coldfusion coldfusion-9






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 24 '11 at 17:06









ale

5,83155264




5,83155264










asked Dec 24 '11 at 4:01









NietzscheNietzsche

84117




84117













  • You should really be using cfqueryparam. Never insert user-accessible data directly into a query. You are risking a sql injection attack. Replace '#form.phone_number#' with <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.phone_number#" />

    – Sean Coyne
    Dec 28 '11 at 0:01



















  • You should really be using cfqueryparam. Never insert user-accessible data directly into a query. You are risking a sql injection attack. Replace '#form.phone_number#' with <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.phone_number#" />

    – Sean Coyne
    Dec 28 '11 at 0:01

















You should really be using cfqueryparam. Never insert user-accessible data directly into a query. You are risking a sql injection attack. Replace '#form.phone_number#' with <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.phone_number#" />

– Sean Coyne
Dec 28 '11 at 0:01





You should really be using cfqueryparam. Never insert user-accessible data directly into a query. You are risking a sql injection attack. Replace '#form.phone_number#' with <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.phone_number#" />

– Sean Coyne
Dec 28 '11 at 0:01












1 Answer
1






active

oldest

votes


















8














The way I've chosen to deal with this issue is to store raw numbers in the database, and then display them how I want on a web page. To normalize the numbers, use a simple regex:



<cfset cleanPhoneNumber = reReplace(form.phoneNumber, "[^0-9]", "", "ALL")>


Store cleanPhoneNumber in the database, and then use a simple format function to display it:



<cffunction name="formatPhoneNumber">
<cfargument name="phoneNumber" required="true">

<cfif len(phoneNumber) EQ 10>
<!--- This only works with 10-digit US/Canada phone numbers --->
<cfreturn "(#left(phoneNumber, 3)#) #mid(phoneNumber, 4, 3)#-#right(phoneNumber, 4)#">
</cfif>

<cfreturn phoneNumber>
</cffunction>


The challenge is that outside the US there is not a standardized way to format numbers (that I've found). Also be careful if you allow a user to enter an extension in the phone number field.






share|improve this answer


























  • Thanks, this works well.

    – Nietzsche
    Dec 24 '11 at 4:54











  • I would add, don't clean the number before putting into the DB, clean it when you display it to yourself or others. That way you always have the option to show them what they entered.

    – Adrian Lynch
    Dec 24 '11 at 20:50






  • 1





    I store both the phone number as entered and a cleaned up version. I have an index in the DB on the cleaned version so i can search by phone number a lot faster.

    – Yisroel
    Dec 25 '11 at 1:14











  • How do you output the formatted phone number?

    – aparker81
    Nov 26 '12 at 17:08











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8622587%2fstandardize-phone-numbers-in-submitted-form-with-coldfusion%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









8














The way I've chosen to deal with this issue is to store raw numbers in the database, and then display them how I want on a web page. To normalize the numbers, use a simple regex:



<cfset cleanPhoneNumber = reReplace(form.phoneNumber, "[^0-9]", "", "ALL")>


Store cleanPhoneNumber in the database, and then use a simple format function to display it:



<cffunction name="formatPhoneNumber">
<cfargument name="phoneNumber" required="true">

<cfif len(phoneNumber) EQ 10>
<!--- This only works with 10-digit US/Canada phone numbers --->
<cfreturn "(#left(phoneNumber, 3)#) #mid(phoneNumber, 4, 3)#-#right(phoneNumber, 4)#">
</cfif>

<cfreturn phoneNumber>
</cffunction>


The challenge is that outside the US there is not a standardized way to format numbers (that I've found). Also be careful if you allow a user to enter an extension in the phone number field.






share|improve this answer


























  • Thanks, this works well.

    – Nietzsche
    Dec 24 '11 at 4:54











  • I would add, don't clean the number before putting into the DB, clean it when you display it to yourself or others. That way you always have the option to show them what they entered.

    – Adrian Lynch
    Dec 24 '11 at 20:50






  • 1





    I store both the phone number as entered and a cleaned up version. I have an index in the DB on the cleaned version so i can search by phone number a lot faster.

    – Yisroel
    Dec 25 '11 at 1:14











  • How do you output the formatted phone number?

    – aparker81
    Nov 26 '12 at 17:08
















8














The way I've chosen to deal with this issue is to store raw numbers in the database, and then display them how I want on a web page. To normalize the numbers, use a simple regex:



<cfset cleanPhoneNumber = reReplace(form.phoneNumber, "[^0-9]", "", "ALL")>


Store cleanPhoneNumber in the database, and then use a simple format function to display it:



<cffunction name="formatPhoneNumber">
<cfargument name="phoneNumber" required="true">

<cfif len(phoneNumber) EQ 10>
<!--- This only works with 10-digit US/Canada phone numbers --->
<cfreturn "(#left(phoneNumber, 3)#) #mid(phoneNumber, 4, 3)#-#right(phoneNumber, 4)#">
</cfif>

<cfreturn phoneNumber>
</cffunction>


The challenge is that outside the US there is not a standardized way to format numbers (that I've found). Also be careful if you allow a user to enter an extension in the phone number field.






share|improve this answer


























  • Thanks, this works well.

    – Nietzsche
    Dec 24 '11 at 4:54











  • I would add, don't clean the number before putting into the DB, clean it when you display it to yourself or others. That way you always have the option to show them what they entered.

    – Adrian Lynch
    Dec 24 '11 at 20:50






  • 1





    I store both the phone number as entered and a cleaned up version. I have an index in the DB on the cleaned version so i can search by phone number a lot faster.

    – Yisroel
    Dec 25 '11 at 1:14











  • How do you output the formatted phone number?

    – aparker81
    Nov 26 '12 at 17:08














8












8








8







The way I've chosen to deal with this issue is to store raw numbers in the database, and then display them how I want on a web page. To normalize the numbers, use a simple regex:



<cfset cleanPhoneNumber = reReplace(form.phoneNumber, "[^0-9]", "", "ALL")>


Store cleanPhoneNumber in the database, and then use a simple format function to display it:



<cffunction name="formatPhoneNumber">
<cfargument name="phoneNumber" required="true">

<cfif len(phoneNumber) EQ 10>
<!--- This only works with 10-digit US/Canada phone numbers --->
<cfreturn "(#left(phoneNumber, 3)#) #mid(phoneNumber, 4, 3)#-#right(phoneNumber, 4)#">
</cfif>

<cfreturn phoneNumber>
</cffunction>


The challenge is that outside the US there is not a standardized way to format numbers (that I've found). Also be careful if you allow a user to enter an extension in the phone number field.






share|improve this answer















The way I've chosen to deal with this issue is to store raw numbers in the database, and then display them how I want on a web page. To normalize the numbers, use a simple regex:



<cfset cleanPhoneNumber = reReplace(form.phoneNumber, "[^0-9]", "", "ALL")>


Store cleanPhoneNumber in the database, and then use a simple format function to display it:



<cffunction name="formatPhoneNumber">
<cfargument name="phoneNumber" required="true">

<cfif len(phoneNumber) EQ 10>
<!--- This only works with 10-digit US/Canada phone numbers --->
<cfreturn "(#left(phoneNumber, 3)#) #mid(phoneNumber, 4, 3)#-#right(phoneNumber, 4)#">
</cfif>

<cfreturn phoneNumber>
</cffunction>


The challenge is that outside the US there is not a standardized way to format numbers (that I've found). Also be careful if you allow a user to enter an extension in the phone number field.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 10:35









Mark Skelton

1,77631631




1,77631631










answered Dec 24 '11 at 4:40









Scott ColdwellScott Coldwell

764412




764412













  • Thanks, this works well.

    – Nietzsche
    Dec 24 '11 at 4:54











  • I would add, don't clean the number before putting into the DB, clean it when you display it to yourself or others. That way you always have the option to show them what they entered.

    – Adrian Lynch
    Dec 24 '11 at 20:50






  • 1





    I store both the phone number as entered and a cleaned up version. I have an index in the DB on the cleaned version so i can search by phone number a lot faster.

    – Yisroel
    Dec 25 '11 at 1:14











  • How do you output the formatted phone number?

    – aparker81
    Nov 26 '12 at 17:08



















  • Thanks, this works well.

    – Nietzsche
    Dec 24 '11 at 4:54











  • I would add, don't clean the number before putting into the DB, clean it when you display it to yourself or others. That way you always have the option to show them what they entered.

    – Adrian Lynch
    Dec 24 '11 at 20:50






  • 1





    I store both the phone number as entered and a cleaned up version. I have an index in the DB on the cleaned version so i can search by phone number a lot faster.

    – Yisroel
    Dec 25 '11 at 1:14











  • How do you output the formatted phone number?

    – aparker81
    Nov 26 '12 at 17:08

















Thanks, this works well.

– Nietzsche
Dec 24 '11 at 4:54





Thanks, this works well.

– Nietzsche
Dec 24 '11 at 4:54













I would add, don't clean the number before putting into the DB, clean it when you display it to yourself or others. That way you always have the option to show them what they entered.

– Adrian Lynch
Dec 24 '11 at 20:50





I would add, don't clean the number before putting into the DB, clean it when you display it to yourself or others. That way you always have the option to show them what they entered.

– Adrian Lynch
Dec 24 '11 at 20:50




1




1





I store both the phone number as entered and a cleaned up version. I have an index in the DB on the cleaned version so i can search by phone number a lot faster.

– Yisroel
Dec 25 '11 at 1:14





I store both the phone number as entered and a cleaned up version. I have an index in the DB on the cleaned version so i can search by phone number a lot faster.

– Yisroel
Dec 25 '11 at 1:14













How do you output the formatted phone number?

– aparker81
Nov 26 '12 at 17:08





How do you output the formatted phone number?

– aparker81
Nov 26 '12 at 17:08


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8622587%2fstandardize-phone-numbers-in-submitted-form-with-coldfusion%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith