(as a follow up to this question: In Jquery AND Rails, how to get visible character count of a string?)
Let’s say you have a string with embedded links and tags like so:
This string has <a href="http://www.google.com">some links</a> inside it.
Then the visible string to a user is:
This string has some links inside it.
The full string has 73 characters and the visible just 37 characters.
Let’s say I now want to write a rails validation (custom validation) that checks the visible length of the string. ie:
validate :string_visible_length
def string_visible_length
#some code that verifies the character length of a field named :field_name
end
Any ideas how to accomplish this? I would need to strip the html tags and then check the character length..
I think you could use the strip links method in your validation.
Make helpers available in your model file:
Here’s the validation: