Page 1 of 1
Limit of characters number in the input field
Posted: Thu Jun 06, 2019 12:08 pm
by Janusz
To cut excessive number of characters entered in the input field before saving I use in the custome code of given field the onblur with following code:
Code: Select all
var max_chars = 10; if($('#req_raport_nr').val().length > max_chars) { $('#req_raport_nr').val($('#req_raport_nr').val().substr(0, max_chars))}
where in this case the field ref is req_raport_nr.
I would like to have it as an function where I could use for example onblur with:
Code: Select all
limit_char('req_raport_nr',5)
or even better
limit_char(5) - if it can recognize field ref.
but have some difficulty to properly transfer the origninal code into function the like:
Code: Select all
function limit_char(field,qty) {
which code ??
}
Do you have any suggestions how the function should look like.
Re: Limit of characters number in the input field
Posted: Thu Jun 06, 2019 3:03 pm
by kev1n
Untested:
f
Code: Select all
unction limit_char(field, qty) {
if ($('#' + field).val().length > qty) {
$('#' + field).val($('#' + field).val().substr(0, qty));
}
}
Re: Limit of characters number in the input field
Posted: Thu Jun 06, 2019 3:10 pm
by kev1n
If you want to have something more sophisticated, you can use this function:
Code: Select all
function setFieldMaxCharLimit(field, lblCount, maxlen) {
// Set maxlen for "field" and format remaining characters
$('#' + field).attr('maxlength', maxlen);
$('#' + lblCount).css('color', 'gray');
$('#' + lblCount).find('strong, b').css('font-weight', 'normal');
$('#' + field).keyup(function(e) {
// Finding total characters in Textarea
var txtLen = $(this).val().length;
if (txtLen <= maxlen) {
var remain = maxlen - txtLen;
// Updating remaining character count
$('#' + lblCount).text(remain);
}
});
$('#' + field).keydown(function(e) {
var keycode = e.keyCode;
// Finding total characters in Textarea
var txtLen = $(this).val().length;
if (txtLen > maxlen) {
// when keycode is not of backspace
if (keycode != 8) {
// Stopping new character to enter
// e.preventDefault();
return false;
}
}
});
};
How to use it:
Code: Select all
setFieldMaxCharLimit('req_raport_nr','lbl_count',10);
lbl_count is an object of type word to display the number of remaining characters. Could be removed if you don't need it.
Re: Limit of characters number in the input field
Posted: Thu Jun 06, 2019 3:33 pm
by Janusz
Thanks a lot - works perfectly.
Re: Limit of characters number in the input field
Posted: Fri Jun 07, 2019 10:02 am
by admin
.