Textarea Limit words per line Jquery or Javascript

Simeon Stoyanov

I have a textarea field in my view.

I need to set per line only 1 word.

If the user tries to enter second word on the same line the word should be on the next one. (does not matter if he presses enter or space)

Also how can I restrict the user from typing comas.

The idea is only 1 word per line nothing more.

How to set these restrictions using jquery or javascript?

Thank you!

gargsms

You can try to change event.target.value on the keyup events on the textarea.

You can do something like, assuming your textarea has an ID of #textArea,

document.getElementById( 'textArea' ).onkeyup = function ( e ) {
  e.target.value = e.target.value.replace( ' ', '\n' )
    .replace( ',', '' );
}

This would replace all your spaces to \n, which is a new line, and also take rid of all the commas.

I would go on to say onkeyup is not the most elegant method to do this. You should instead use addEventListener( 'keyup', callback )

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related