Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create space in input box when typing 1

Status
Not open for further replies.

hamish75

Technical User
Dec 18, 2009
39
GB
first of all, i think this is a javascript question, as i think it would fall under an onkeyup or something like that.

does any one know any examples of how to create a space after inputting 3 characters in an input box whilst typing?

for example, if i typed the alphabet it would show like this: ABC DEF HIJ etc...


Ah dinnae ken...
 
Hi

A brutal way is this :
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]space[/color][teal]([/teal]what[teal])[/teal]
[teal]{[/teal]
  what[teal].[/teal]value[teal]=[/teal]what[teal].[/teal]value[teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/ /g[/fuchsia][teal],[/teal][green][i]''[/i][/green][teal]).[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/(...)/g[/fuchsia][teal],[/teal][green][i]'$1 '[/i][/green][teal])[/teal]
[teal]}[/teal]
HTML:
[b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"text"[/i][/green] [maroon]onkeyup[/maroon][teal]=[/teal][green][i]"space(this)"[/i][/green][b]>[/b]
Personally I hate it, because in some browsers the [tt]value[/tt] assignment has the side effect of moving the cursor to the end.

Feherke.
 
You coudl try somethng like this:
Code:
<script>
var i=0;

function Spacer_Format(In_obj, spacer){

if(i<2){
i++;
return true;
}
else{
In_obj.value+=spacer;
i=0;
}



}

</script>

<input type="text" name="mytext" onKeyUp="Spacer_Format(this,' '); return true;">

However you may need to check what keys are being pressed as right now it reacts to every key press. So moving inside the textbox with the arrow keys is kind of annoying.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi

Phil said:
So moving inside the textbox with the arrow keys is kind of annoying.
Not mentioning that you are again supposing that the user will only append new characters at the end of the value. If the user will insert at the beginning or in the middle, the new spaces are still appended to the end, not related to the new character's position.

Feherke.
 
thank you feherke, works great using firefox and ie.

Personally I hate it, because in some browsers the value assignment has the side effect of moving the cursor to the end.

i have to carry out some other testing in other browsers.

Ah dinnae ken...
 
True. But of course this type of deal requires a lot of checking and conditional work to make it look seamless and still function as expected.

The Other option is to provide separate text boxes for the different parts of the input and then programmatically join them with the spaces in place.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top