0

Due to a bug on the application I am attempting to strip leading and trailing whitespace from any input. It is a ruby on rails application however I am not sure what approach to take. The main point is not how to achieve this but rather which is the better approach to take in terms of efficiency etc.

JQuery

With JQuery I can just add the following:

$("input, select").change(function()
{
  this.value=$(this).val().trim();
});

With Ruby I can always use strip. What would the best approach be? I believe JQuery as its simple and effective however I default to JQuery generally as my knowledge of Ruby Rails is limited.

EamonnMcElroy
  • 587
  • 6
  • 20

2 Answers2

0

To remove the leading and trailing whitespaces you can use.

value.strip

Less code is better, in my opinion.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
Daniel B
  • 72
  • 1
  • 9
  • 1
    I mean to apply this globally to the site. Because jquery runs on any input change then I thought it would be more effective. If I was to use ruby how could i apply this globally and what would the advantage be? – EamonnMcElroy Aug 04 '16 at 11:22
  • I guess, in that case, the jQuery way seems good. It'll deal with all the entries globally rather than for each value. So in that case, you'll have less code with jQuery. – Daniel B Aug 04 '16 at 11:25
  • They jQuery way will deal with whitespaces before they are submitted (client-side). Ruby can't do that. So I would go with the jQuery way if you mean to use it on all inputs. – Daniel B Aug 04 '16 at 11:31
0

Yeah you can try this as well:

value.lstrip.rstrip

This will remove leading and trailing spaces.

Aamir
  • 16,329
  • 10
  • 59
  • 65
SnehaT
  • 309
  • 4
  • 14
  • I understand the options but what I am asking is which is the best approach for speed and functionality. – EamonnMcElroy Aug 04 '16 at 11:28
  • Both are good....If you have something to do where after removing the whitespaces you need to perform some operation, you can do this in common lib in ROR and thus would be global for all...Its a server side thing.. – SnehaT Aug 04 '16 at 11:35