Best option - Stripping whitespace using Rails or JQuery

127 views Asked by At

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.

2

There are 2 answers

3
Daniel B On

To remove the leading and trailing whitespaces you can use.

value.strip

Less code is better, in my opinion.

2
SnehaT On

Yeah you can try this as well:

value.lstrip.rstrip

This will remove leading and trailing spaces.