How can text area contents can be replaced to "*" by skipping the new line character

textarea value
javascript find and replace in textarea
replace text in textarea javascript
jquery find and replace text in textarea
how to display html content in textarea using javascript
how to print value in textarea using javascript
how to save textarea value in html
set textarea value javascript

I have a requirement where I need to replace all the text into "" (basically masking) at run time entering into jtextArea. Mean to say I am storing the key into a variable at textArea displaying that as "". Got a code for that which is as :

public class App {
            String text= "";
            App(){
                JFrame f= new JFrame();
               // final JTextArea area=new JTextArea();
                JTextArea jTextArea=new JTextArea();

                jTextArea.addKeyListener(new KeyListener() {
                public void keyTyped(KeyEvent e) {

                }

                public void keyPressed(KeyEvent e) {

                }

                public void keyReleased(KeyEvent e) {
                   if(Character.isLetter(e.getKeyChar())) {
                  System.out.println(text);
                     //text = text + e.getKeyChar();
                       text += String.valueOf(e.getKeyChar());
                       System.out.println(text);
                       jTextArea.setText(text.replaceAll("[a-z]", "*"));
                   }
                   //System.out.println(text);
               }

            });
                System.out.println(text);
            jTextArea.setBounds(10,30, 200,200);
            jTextArea.setAutoscrolls(true);
            f.add(jTextArea);
            f.setSize(300,300);
            f.setLayout(null);
            f.setVisible(true);
        }
        public static void main(String args[])
        {
            new App();
        }

}

Here the problem is its converting my "New line" too and adding that to first line itself.

What my requirement is :

Input :

Hello

how
are

you

Should appear as :

*****

***
***

***

Kindly advise


Your mistake is that you are not appending new line characters to the text variables, which means that text will always have letters, but never new lines. Since you set the text field's text to be text, text should have those new line characters too.

You should just remove the if statement, as the check is already being "done" by the regex [a-z] in the replaceAll call.

public void keyReleased(KeyEvent e) {
    text += String.valueOf(e.getKeyChar());
    jTextArea.setText(text.replaceAll("[a-z]", "*"));
}

However, this still doesn't handle stuff like backspace and pasting, not to mention that it would be better if text were a StringBuilder. But I'll leave it to you to improve your code.

Find and Replace for an Textarea, Replace All: will replace all occurences and select the piece of text that has been <script src="jquery.js" type="text/javascript"></script> <textarea <input type=" text" id="termReplace" name="termReplace" value="solution"� In this case, the image is a friendly reminder to be nice =). If you add a background image, for whatever reason, it can break the browser default styling of the textarea. The default 1px solid bolder is replaced with a thicker beveled border. To restore the browser default, you can just force the border back to normal.


Check below code if this is what you are looking for

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class App {
            String text= "";
            App(){
                JFrame f= new JFrame();
               // final JTextArea area=new JTextArea();
                JTextArea jTextArea=new JTextArea();

                jTextArea.addKeyListener(new KeyListener() {
                public void keyTyped(KeyEvent e) {

                }

                public void keyPressed(KeyEvent e) {

                }

                public void keyReleased(KeyEvent e) {
                       text += String.valueOf(e.getKeyChar());
                       System.out.println(text);
                       jTextArea.setText(text.replaceAll("[a-z]", "*"));
               }

            });
                System.out.println(text);
            jTextArea.setBounds(10,30, 200,200);
            jTextArea.setAutoscrolls(true);
            f.add(jTextArea);
            f.setSize(300,300);
            f.setLayout(null);
            f.setVisible(true);
        }
        public static void main(String args[])
        {
            new App();
        }

}

Javascript: how to replace word in textarea, value = newText; will do the job. There is no default method with a string parameter to replace multiple occurences. But the regex version can do� The onchange JavaScript event is triggered when an element is changed and then loses focus. In the context of a textarea, this happens when the content of the textarea is modified and then the textarea loses focus because the user clicks away or presses the tab key.


Since the other two answers both have the problem of only replacing alphabetical input, therefore ignoring all numbers, special characters or whitespaces, I thought I'd add on as well. If you actually just want to replace ANY character to a star, you don't even have to play around with RegEx and replacing characters, you might as well just append a star every time keyReleased event is called.

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class App
{
    String displayText = "";
    String valueText = "";

    App()
    {
        JFrame f = new JFrame();
        // final JTextArea area=new JTextArea();
        JTextArea jTextArea = new JTextArea();

        jTextArea.addKeyListener(new KeyListener()
        {
            public void keyTyped(KeyEvent e)
            {
            }

            public void keyPressed(KeyEvent e)
            {

            }

            public void keyReleased(KeyEvent e)
            {
                if(e.getKeyCode() != KeyEvent.VK_ENTER)
                {
                    displayText += "*";
                    valueText += String.valueOf(e.getKeyChar());
                    jTextArea.setText(displayText);
                }
            }

        });
        System.out.println(valueText);
        jTextArea.setBounds(10, 30, 200, 200);
        jTextArea.setAutoscrolls(true);
        f.add(jTextArea);
        f.setSize(300, 300);
        f.setLayout(null);
        f.setVisible(true);
    }

    public static void main(String args[])
    {
        new App();
    }

}

If you want to keep the typed in value in the backend, just save the typed in value somewhere and only display text.

<textarea>, If this value isn't specified, the user can enter an unlimited number of <textarea > is a replaced element — it has intrinsic dimensions, like a� On one of my pages, I have a text area html tag for users to write a letter in. I want the content below the text area to shift down, or in other words, I want the text area to resize vertically with each line added to the text area and to have the content below simply be positioned in relation to the bottom of the text area.


This is complete code which worked as per expectation :

import javax.swing.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;

    public class App {
            //String text= "";
        String text = "";
            App(){
                JFrame f= new JFrame();
               // final JTextArea area=new JTextArea();
                JTextArea jTextArea=new JTextArea();

                jTextArea.addKeyListener(new KeyListener() {
                public void keyTyped(KeyEvent e) {

                }

                public void keyPressed(KeyEvent e) {

                }

                public void keyReleased(KeyEvent e) {
                    char ch = e.getKeyChar();
                    if (e.getExtendedKeyCode() == KeyEvent.VK_BACK_SPACE) {
                        //System.out.println("It Works Backspace pressed..!");
                    }
                    else {
                    text += String.valueOf(ch);
                    }
                    //System.out.println(e.getKeyChar());
                    jTextArea.setText(text.replaceAll(".", "*"));
                    //jTextArea.setText(text.replaceAll(".[\b]", "*"));
                    //jTextArea.setText(text.toString().replaceAll("[\\b].", "*"));
                }

            });
                System.out.println(text);
            jTextArea.setBounds(10,30, 200,200);
            jTextArea.setAutoscrolls(true);
            f.add(jTextArea);
            f.setSize(300,300);
            f.setLayout(null);
            f.setVisible(true);
        }
        public static void main(String args[])
        {
            new App();
        }

}

How to set or update value of textarea with JavaScript/jQuery , 1. JavaScript. A textarea doesn't have a value attribute, unlike an input field. With jQuery, you can use .text() method to replace the textarea's text. JS; HTML� If you need form fields to accept email addresses, phone numbers, and other content that is easy to define, use the appropriate input type. However, when you need a field that can accept any combination and length of plain text letters, numbers, and symbols, <textarea> is the element you are looking for. HTML5 Textarea Attributes


How to change the Content of a <textarea> using JavaScript , Given an HTML document containing a <textarea> element and the task is to change Method 1: This method uses id attribute of textarea with value property to How to detect If textbox content has changed using JavaScript ? If you like GeeksforGeeks and would like to contribute, you can also write an� Text areas are editable by default. The code setEditable(false) makes the text area uneditable. It is still selectable and the user can copy data from it, but the user cannot change the text area's contents directly. The following code adds text to the text area.


HTML5 Textarea Attributes: Here's What You Should Know �, <textarea placeholder="You can type into this textarea all day long if you'd like to, be changed</p> <textarea cols="50" rows="3" readonly>This value can't be� It can be done with CSS3’s box-sizing property. You would set the value to border-box and then it will account for the borders and padding (everything from the outer points of the border inward).


Textarea Tricks, You can add a background-image to a textarea like you can any other element. The default 1px solid bolder is replaced with a thicker beveled border. it's not a real form element anymore so you'll need JavaScript to extract/send the value. Given an HTML document containing a <textarea> element and the task is to change the content of a <textarea> element with the help of JavaScript. Method 1: This method uses id attribute of textarea with value property to change the content of <textarea> element.