Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Text Box Phone Number

Author  Topic 

cjhardie
Yak Posting Veteran

58 Posts

Posted - 2006-09-21 : 09:43:32
Is there any way to make a textbox into the format of ###-#### so the hyphen is always there or a way for an error message to prompt if someone did not insert the hyphen??

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-09-21 : 09:47:14
I think there is a FORMAT something property you can set...


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

dfiala
Posting Yak Master

116 Posts

Posted - 2006-09-21 : 10:02:52
There is no native masked control to do this. You either need to:
1) use a third party control, either license one or use something like this: http://www.codeproject.com/useritems/Xtended_TextBox.asp
2) add a regex validator to your form to check for the proper format
3) add the hyphen yourself before you save the data
I'd favor option 3. If the user enters 7 digits, you know where to stick the hyphen, why force the user to go through the effort



Dean Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2006-09-21 : 10:04:10
i'd go with #2. regex is a best bet.
3 isn't as good because users are set in their ways and they don't want to change.



Go with the flow & have fun! Else fight the flow
blog thingie: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

dfiala
Posting Yak Master

116 Posts

Posted - 2006-09-21 : 10:10:38
That's exactly why you go with 3, don't make the users change or think. Let 'em enter the info any old way that the software can figure out.

http://www.amazon.com/About-Face-2-0-Essentials-Interaction/dp/0764526413/sr=8-1/qid=1158847818/ref=pd_bbs_1/103-2982630-1495037?ie=UTF8&s=books

Dean Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2006-09-21 : 10:18:18
well i understood you that in #3 you meant to allow them to enter only numbers.

but if you add a hyphen yourself, then what happens if a user already enters the hyphen?



Go with the flow & have fun! Else fight the flow
blog thingie: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

dfiala
Posting Yak Master

116 Posts

Posted - 2006-09-21 : 10:24:08
Then they've already added the hyphen. Simple enough to check for. Only add it if necessary.

Dean Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2006-09-21 : 11:08:46
and we're back to option 2
if a user enters a ( or ) or - you'd have to hande each with an if.
using regex solves this.

NEVER trust a user's entry.



Go with the flow & have fun! Else fight the flow
blog thingie: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

dfiala
Posting Yak Master

116 Posts

Posted - 2006-09-21 : 11:17:36
Exactly! But why force the user to enter a hyphen?

Sigh. Why are you making the user work instead of making the code work?

You don't even need to use an if statement
Strip what you get at the back end to the bare numbers (easily done with regex) and either enter a hyphen or don't -- but don't make the user have to worry about formatting that is meaningless to the data.



Dean Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2006-09-21 : 11:29:15
i'm saying that some users have it in their blood to type a phone number
as (123) 112-34232-321 or some other format

I agree with you because my original understanding was that you meant only numbers be allowed into the textbox



Go with the flow & have fun! Else fight the flow
blog thingie: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

dfiala
Posting Yak Master

116 Posts

Posted - 2006-09-21 : 11:42:59
All you care about is:
12311234232321

This is the phone numebr.

Everything else is formatting.

So they type it in with hyphens
or parens
or dots
or spaces
Or they don't
Why force them to enter it in your formatting?
Get only the numbers from the string that was entered.
If it has a viable number of digits, use it, format it, save it. If you can't make it into a phone number, ie, it only has 6 digits, then warn them or even refuse to save it if you must, but why make the user enter the same data twice because they forgot a hyphen -- or added a space? Make your code smarter so the user doesn't have to work harder.







Dean Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-09-21 : 12:09:47
The easiest solution is to break it out for them and force them to enter the multiple parts into multiple textboxes, each restricted to only allow the correct content.

i.e., display the form like this to them:

Your Phone Number: ( [xxx] ) [xxx] - [xxxx]


where each [xx] is a separate text box that only accepts that portion of the number .. . Make each one only allow digits, and only the correct count (3 or 4) . Make it clear what data is expected in which textbox. Put the symbols right on the page for them between the text boxes, and/or label them clearly. The biggest inconvienance is that they have to press TAB between each section of the number to move from textbox to textbox. Add another textbox for "Extension" if you think that might be necessary.

Then, store your data in 3 (or 4) columns in the database, with each part of the phone number broken out (area code / exchange / number -- and/or extension as well). Now, you have good data, no symbols, no parsing, and you can easily output or format those parts into a phone number any way you want. And each part is easy to validate as well.

- Jeff
Go to Top of Page
   

- Advertisement -