Limit of characters number in the input field

Questions related to customising nuBuilder Forte with Javascript or PHP.

Limit of characters number in the input field

Postby Janusz » Thu Jun 06, 2019 7:38 pm

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.
Janusz
 
Posts: 166
Joined: Fri Dec 28, 2018 10:11 pm
Location: Krakow, Poland

Re: Limit of characters number in the input field

Postby kev1n » Thu Jun 06, 2019 10:33 pm

Untested:

f
Code: Select all
unction limit_char(field, qty) {

    if ($('#' + field).val().length > qty) {
        $('#' + field).val($('#' + field).val().substr(0, qty));
    }
}
kev1n
 
Posts: 185
Joined: Mon Oct 15, 2018 2:13 am

Re: Limit of characters number in the input field

Postby kev1n » Thu Jun 06, 2019 10:40 pm

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.
Last edited by kev1n on Tue Jun 11, 2019 10:15 pm, edited 1 time in total.
kev1n
 
Posts: 185
Joined: Mon Oct 15, 2018 2:13 am

Re: Limit of characters number in the input field

Postby Janusz » Thu Jun 06, 2019 11:03 pm

Thanks a lot - works perfectly.
Janusz
 
Posts: 166
Joined: Fri Dec 28, 2018 10:11 pm
Location: Krakow, Poland

Re: Limit of characters number in the input field

Postby admin » Fri Jun 07, 2019 5:32 pm

.
admin
Site Admin
 
Posts: 3070
Joined: Mon Jun 15, 2009 9:53 am


Return to Custom Code