How do i add a space to a phone number in a html form? |
How do i add a space to a phone number in a html form? |
dariank |
Jan 12 2023, 10:59 PM
Post
#1
|
Group: Members Posts: 6 Joined: 12-January 23 Member No.: 28,744 |
When the input is 1234567890 it outputs 1234 567 890
Thanks! |
pandy |
Jan 12 2023, 11:07 PM
Post
#2
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,730 Joined: 9-August 06 Member No.: 6 |
You mean the user types it without spaces and you get it with spaces? Can't be done with HTML. You need some kind of programming.
|
dariank |
Jan 13 2023, 03:05 AM
Post
#3
|
Group: Members Posts: 6 Joined: 12-January 23 Member No.: 28,744 |
Ok how do I go about doing that? Can you add some reference code?
|
Christian J |
Jan 13 2023, 10:10 AM
Post
#4
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
When the input is 1234567890 it outputs 1234 567 890 You mean after the form is submitted (the easiest to do), or on the fly while the user is typing (harder)? You can also "force" the user to type it right from the start, using e.g. the INPUT element's PATTERN attribute. Here are some examples: https://www.html5pattern.com/Phones --however in theory a user could circumvent such form requirements, anything important needs to be done with a server-side script. |
pandy |
Jan 13 2023, 03:34 PM
Post
#5
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,730 Joined: 9-August 06 Member No.: 6 |
Yeah, I forgot about pattern. That's probably the best option. So it can after all be done with HTML. Sorry.
|
Christian J |
Jan 13 2023, 06:21 PM
Post
#6
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
But note that PATTERN is just client-side form validation, taking effect after the user tries to submit the form, and as such doesn't really help the user. In fact, being forced to enter the phone number in a certain format might be regarded as unnecessary and annoying. Perhaps spaces will help users spot typos, but nothing prevents users from adding such spaces themselves if they feel the need.
OTOH, if the site owner wants a certain format on the submitted phone number, I think it's more reliable to reformat it with the server-side script. That way the users don't have to be involved with it at all. |
coothead |
Jan 14 2023, 05:56 AM
Post
#7
|
Advanced Member Group: Members Posts: 206 Joined: 12-January 23 From: chertsey, a small town 25 miles south west of london, england Member No.: 28,743 |
Hi there dariank,
coothead |
Brian Chandler |
Jan 16 2023, 02:41 AM
Post
#8
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
Please also notice that except in *extremely* limited cases, trying to force a phone number input like this is antisocial, and totally stupid. For example, if you are an American in America, and you only permit other Americans in America to access your website, then go ahead and enforce the American phone number format. But in all other cases you will just be causing pain for people who live in different countries than the one you are familiar with.
|
coothead |
Feb 7 2023, 09:33 AM
Post
#9
|
Advanced Member Group: Members Posts: 206 Joined: 12-January 23 From: chertsey, a small town 25 miles south west of london, england Member No.: 28,743 |
Hi there Estherkincaid,
to the best of my knowledge, I do not believe that the HTML input elements can be styled with the "before" or "after" content properties. Even if it was possible, "after" does mean after and definitely not between. coothead This post has been edited by coothead: Feb 7 2023, 09:38 AM |
Christian J |
Feb 7 2023, 10:46 AM
Post
#10
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
Please also notice that except in *extremely* limited cases, trying to force a phone number input like this is antisocial, and totally stupid. For example, if you are an American in America, and you only permit other Americans in America to access your website, then go ahead and enforce the American phone number format. But in all other cases you will just be causing pain for people who live in different countries than the one you are familiar with. Not only that, there may also be individual user preferences within a country (regardless of any official format rules). For example, a number like 112233 might be best formatted as "11 22 33", while a number like 111222 might look better as "111 222". Despite this I thought it was an interesting programming exercise, but ran into problems rightaway. First, the user may get stuck if trying to make corrections with the Backspace key, and the script keeps reinserting a space character as soon as the user erased it. Second, what if the user prefers to use hypens instead of spaces, and the script insists on deleting them and insert spaces instead? While none of these issues are catastrophic, they could be potentially annoying, and as such perhaps even cause the user to make typos... IMO, mandatory client-side formatting like should only exist to help the user, but how helpful is it if you try to force the user to use a formatting he's unused to? |
pandy |
Feb 7 2023, 11:41 AM
Post
#11
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,730 Joined: 9-August 06 Member No.: 6 |
Yes. And cell phone numbers can be longer that land line numbers. Here land line number have a three digit county number followed by a 6 digit number. Cell phone numbers were the same to start with, 9 digits. Now they can have totally 10 digits. But many people, including myself, prefer to put the spaces so 4 numbers end up in the beginning group, 0123 12 34 56, makes it easier to remember. When the number is spaced out differently it takes some extra checking to make sure it's correct.
The address book in my phone breaks numbers up in a way I don't like. Annoying. |
Christian J |
Feb 7 2023, 03:31 PM
Post
#12
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
Yes. And cell phone numbers can be longer that land line numbers. Here land line number have a three digit county number followed by a 6 digit number. My old phone dictionary used the format "11 22 33" for local numbers. My current phone's address book displays the same number as "112-233", sometimes with hyphens, sometimes not. There are (were) also the 5 digit automatic response numbers, like 90 510 (speaking clock) or 90 000 (former emergency number), not that you're likely to enter them in a form. QUOTE many people, including myself, prefer to put the spaces so 4 numbers end up in the beginning group, 0123 12 34 56, makes it easier to remember. When the number is spaced out differently it takes some extra checking to make sure it's correct. The address book in my phone breaks numbers up in a way I don't like. Annoying. Indeed, can't remember if it was a problem with my older mobile phones though. |
pandy |
Feb 8 2023, 01:03 AM
Post
#13
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,730 Joined: 9-August 06 Member No.: 6 |
Hi there Estherkincaid, to the best of my knowledge, I do not believe that the HTML input elements can be styled with the "before" or "after" content properties. Even if it was possible, "after" does mean after and definitely not between. coothead Estherkincaid turned out to be a spammer so her posts were removed. |
coothead |
Feb 8 2023, 05:42 AM
Post
#14
|
Advanced Member Group: Members Posts: 206 Joined: 12-January 23 From: chertsey, a small town 25 miles south west of london, england Member No.: 28,743 |
|
Lo-Fi Version | Time is now: 24th April 2024 - 06:27 AM |