1

I'm building a form in a module for Drupal 7 and I want to add a textarea as I would like it to be multiline so that text overflows on the next line, etc. but I don't want users to be able to create a new line if that makes sense. How can I achieve this?

Also, I would like to add a "Select All" button for the textarea and I have followed this article but it doesn't seem to have worked.

I know have the first part of my question solved, but still can't get the "Select All" button working. I have confirmed that the Javascript function does run by adding console.log, but it doesn't do the job. All that happens is the page reloads and no text is selected in the text area.

3
  • 2
    If you are familiar with adding javascript with #attached and Drupal behaviors, you can directly use snippet in stackoverflow.com/questions/425274/… . Showing your current form builder function would help others to suggest a complete answer.
    – AKS
    Aug 26, 2014 at 13:01
  • Please tell us what you have tried. If the question is about code you wrote, for which you want to write better code, or you want to understand why it doesn't work, show the code you are using, or the part of the code that is relevant for the question. - see Help Center.
    – Mołot
    Aug 26, 2014 at 13:06
  • Take a look this link may solve your issue. drupal.org/node/80122
    – DRUPWAY
    Aug 26, 2014 at 13:14

1 Answer 1

3

If using jQuery is an option for you, you can do something like this:

$('#yourTextAreaID').keyup( function() {
  $(this).val( $(this).val().replace( /\r?\n/gi, '' ) );
});

This code will replace the newline character on the fly, after the user types it. This code can be added in a custom.js file of your theme that you include in every page of your site, or you can alternatively use a hook_form_FORM_ID_alter in your custom module in conjunction with the drupal_add_js function to add the specific js file only in the specific form's edit page.

Keep in mind that if Javascript is disabled in the user's browser, he will be able to insert newline characters. In order to prevent this, you will have to add a custom validation function in your form. A very good tutorial on how to do this can be found here. The most important parts are:

1) Use a hook_form_FORM_ID_alter (in the tutorial he uses the old-school approach with hook_form_alter).

2) Add an extra validation function with something like:

array_unshift($form['#validate'], 'MYMODULE_form_validate');

3) Write your custom validation function (name it: MYMODULE_form_validate) that checks for \r?\n in the value of the textarea in the $form_state variable and return a descriptive error if there is a newline character.

Regarding the second part of your question, you should add some more information about what exactly didn't work. Did you clear the cache after following the steps? Do you get any errors in the log messages? Did you add console.log in your Javascript to verify that is being executed?

2
  • Thanks for your answer. The jQuery worked perfectly for the first part of my question, and even though I'm going to require that the user has Javascript to use this part of the website I'm going to take a look at that tutorial. I will add more detail to my original question, but I have cleared the cache and I can't see any errors but I've just checked using alert() and that doesn't execute either - the page just refreshes.
    – Andy
    Aug 26, 2014 at 14:18
  • Please see my second update - I have now got the "Select All" button working. I'm accepting your answer as it solved my original question.
    – Andy
    Aug 26, 2014 at 15:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.