Textarea jquery write protection for wildcards

Does anyone know if it is possible to protect a string substitution variable in a text box?

HTML:

<textarea name="mail_text" id="mail_text">
{salutation} {recipient},
thanks for your email.
Regards
{username}
</textarea>

I would like to catch when someone tries to change one of the wildcards: {salutation}, {recipient} and {username}

$("textarea").keyup(function() {
  var val = $(this).val();
  //protect the wildcards
});

Thank!

+3
source share
2 answers

You cannot practically protect part of the text field. In fact, you can try to block the input of clicks when the cursor is inside the template {...}, but there are so many other ways to edit it, for example. select a range, then delete / replace, cut / copy / paste, drag and drop ...

textarea , - , :

<textarea id="mail_text">...</textarea>
<div id="mail_text_warning"></div>

<script type="text/javascript">
    function checkMailText() {
        var tokens= ['username', 'recipient', 'salutation'];
        var value= $('#mail_text').val();
        var problems= [];

        $.each(tokens, function() {
            if (value.split('{'+this+'}').length!==2)
                problems.push('Please ensure there is one and only one {'+this+'} token present in the text');
        });
        matches= value.match(/\{[^\}]*\}/g);
        if (matches!==null) {
            $.each(matches, function() {
                for (var i= tokens.length; i-->0;)
                    if ('{'+tokens[i]+'}'===this)
                        return;
                problems.push('Token '+this+' is not known');
            });
        }

        $('#mail_text_warning').text(problems.join('. '));
    }
    setInterval(checkMailText, 500);
</script>
+2

.
textareas: , ( " . " ..). , . ( keydown keyup).

0

Source: https://habr.com/ru/post/1741250/


All Articles