-1

Using Bootstrap form builder, I want to know how I can restrict user from entering # and . into the property of a label, where contenteditable is true.

I want this to be implemented for any random label that is going to be edited. How to do that?

I am aware of the ascii values for that but for all the labels present in that page when content editable is true i want user not to input # and . into it.

My label sample is this:

<label>
    <strong contenteditable="true">Telephone</strong>
</label>

I tried using this but does not prevent it from typing # & .

$('body').on('paste input', '[contenteditable]', function(data) {
var text = $(this).text();
    if (text == '#' || text == '.'){
        console.log('hi');
    data.preventDefault();
} 
});

working now

$('body').on('paste input', '[contenteditable]', function(data) {
var text = $(this).text();
var regax1 = /#/;
var regax2 = /./;
if (text.match(regax1)) {
   var newText = text.replace('#', '');
   $('strong').text(newText);
        }
    });
});
tjati
  • 5,761
  • 4
  • 41
  • 56
Shaggie
  • 1,799
  • 7
  • 34
  • 63
  • See this [topic](http://stackoverflow.com/questions/1391278/contenteditable-change-events) – doldt Apr 28 '15 at 12:30
  • thanks for the response but this is for the specific field input. In my case there are unknown number of labels depending upon user drag & drop. For all the labels this should be applied how to do it... – Shaggie Apr 28 '15 at 12:34
  • that link describes a general method for attaching event listeners while using the contendEditable attribute. You can use that to listen on any elements - a specific field input, if need be. Am I missing something? – doldt Apr 28 '15 at 12:56
  • i have edited the post to show ine of the label. check it. – Shaggie Apr 28 '15 at 13:09
  • Formatting text, removing unnecessary tag. – tjati Apr 30 '15 at 14:27

2 Answers2

0

I don't know bootstrap form builder but you can do this easily using onkeyup event on every input.

When user press a key, look keycode and if you don't like this key, remove the last character. Or on every onkeyup use regexp to replace bad character.

  • Maybe `preg_replace()`, because the user might copy a word containing the forbidden characters, and the script would not detect them. – Ciprian Apr 28 '15 at 13:23
0

There you go:

jQuery(document).ready(function($) {
    $('body').on('paste input', '[contenteditable]', function(data) {
        var text = $(this).text();

        if(text.match(/#/g).length > 0) {
            var newText = text.replace('#', '');
            $('strong').text(newText);
        } 
    });
});

Here's a pen: http://codepen.io/ciprian/pen/BNyqbv

Ciprian
  • 872
  • 1
  • 10
  • 30