parent
df1b101aa2
commit
a1b93f256d
@ -1,28 +1,24 @@
|
||||
$(document).ready(function() {
|
||||
setTextAreaLimit();
|
||||
|
||||
function setTextAreaLimit() {
|
||||
$.fn.extend({
|
||||
limiter: function(limit, elem) {
|
||||
$('textarea').on("keyup focus show", function() {
|
||||
setCount(this, elem);
|
||||
$.fn.extend({
|
||||
characterLimiter: function(limit, $label) {
|
||||
$(this).on("keyup focus show", function() {
|
||||
setCount(this, $label);
|
||||
});
|
||||
|
||||
function setCount(src, elem) {
|
||||
// TODO: append label container after $this, instead of hard codeing
|
||||
function setCount(src, $label) {
|
||||
if(src !== undefined) {
|
||||
var chars = src.value.length;
|
||||
if (chars > limit) {
|
||||
if (chars >= limit) {
|
||||
src.value = src.value.substr(0, limit);
|
||||
chars = limit;
|
||||
}
|
||||
elem.html(limit - chars);
|
||||
$label.html(limit - chars);
|
||||
}
|
||||
}
|
||||
setCount($(this)[0], elem);
|
||||
}
|
||||
});
|
||||
|
||||
var elem = $(".chars span");
|
||||
$('textarea').limiter(1000, elem);
|
||||
setCount($(this)[0], $label);
|
||||
}
|
||||
});
|
||||
|
||||
$(document).ready(function() {
|
||||
$('textarea').characterLimiter(1000, $(".chars span"));
|
||||
});
|
||||
|
@ -6,7 +6,7 @@ class AnswerFormatValidator < ActiveModel::EachValidator
|
||||
private
|
||||
|
||||
def text record, attribute, value
|
||||
clean_val = value.to_s.strip
|
||||
clean_val = value.to_s.strip.delete("\r")
|
||||
return if clean_val.length.between?(1, 1000)
|
||||
|
||||
if clean_val.blank?
|
||||
|
@ -25,11 +25,24 @@ class AnswerFormatValidatorTest < ActiveSupport::TestCase
|
||||
assert_match(/enter.*answer/, obj.errors.messages[:answer][0])
|
||||
end
|
||||
|
||||
test "text should FAIL with more than 1000 charactures" do
|
||||
test "text should PASS with 999 character" do
|
||||
obj = AnswerValidatable.new('text')
|
||||
obj.answer = SecureRandom.urlsafe_base64(1001)
|
||||
obj.answer = long_string_with_returns
|
||||
|
||||
assert obj.valid?
|
||||
assert obj.errors.messages.empty?
|
||||
end
|
||||
|
||||
test "text should FAIL with more than 1000 character" do
|
||||
obj = AnswerValidatable.new('text')
|
||||
obj.answer = long_string_with_returns + " - to long now "
|
||||
|
||||
refute obj.valid?
|
||||
assert_match(/char.*limit.*1000.$/, obj.errors.messages[:answer][0])
|
||||
end
|
||||
|
||||
def long_string_with_returns
|
||||
# returns 999 chars, after \r is stripped.
|
||||
"Some rando input\r\n\rYo. Making this up.\r\n" * 27
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user