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 '-').
Asked
Active
Viewed 83 times
2
-
Not in CSS, but yes in Javascript – Marcos Pérez Gude May 27 '16 at 12:06
-
You should do it with Javascript as I do not think it can be possible on CSS. – Francisco Romero May 27 '16 at 12:06
-
Use plugins for that: http://www.igorescobar.com/blog/2012/05/06/masks-with-jquery-mask-plugin/ – Justinas May 27 '16 at 12:09
-
Could you suggest me how to do it in Jacascript? – barmi May 27 '16 at 12:09
-
http://jsfiddle.net/juspC/119/ - got this from http://stackoverflow.com/q/19112011/6049581 - might be useful :) – Frits May 27 '16 at 12:19
2 Answers
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:

trinaldi
- 2,872
- 2
- 32
- 37
-
-
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