2

Is there any way to set 'const' chars to text in css or html? For Example user writes phone number 646830293 and in text (one input) this is 646-830-293 (so every fourth and eighth chars are '-').

barmi
  • 665
  • 6
  • 22

2 Answers2

1

I think you cannot do it with pure CSS but it is very simple only using Javascript.

var text = document.getElementById('text').innerHTML;

var output = text.substring(0, 3) + "-" + text.substring(3, 6) + "-" + text.substring(6);

document.getElementById('output').innerHTML = output;
<p id="text">646830293</p>
<p id="output"></p>
Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
1

jQuery Mask Plugin might the solution for you. It's very simple to use and has very good customization.

From the documentation, you'd write a simple mask with:

$(document).ready(function(){
  $('.date').mask('00/00/0000');
  $('.time').mask('00:00:00');
  $('.date_time').mask('00/00/0000 00:00:00');
});

The whole page has a lot more examples!

Using your pastebin, here's the result:

jQuery Mask working

trinaldi
  • 2,872
  • 2
  • 32
  • 37
  • What should I include in html file for use it? – barmi May 27 '16 at 13:09
  • Since they don't have a CDN (AFAIK), you have to [download](https://github.com/igorescobar/jQuery-Mask-Plugin/archive/v1.14.0.zip) the library then add inside your `` tag with `` ([Very simple explanation](http://javascript.info/tutorial/adding-script-html#external-scripts)) – trinaldi May 27 '16 at 13:14
  • Well I did it but probably there is another problem. Could you check my basic code please? http://pastebin.com/c2S9mWA6 – barmi May 27 '16 at 13:22
  • You're missing jQuery in your file. Here's a pastebin with it: http://pastebin.com/QfhPzfcV – trinaldi May 27 '16 at 13:28