Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Limit of characters number in the input field

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Post Reply
Janusz
nuBuilder Team
Posts: 506
Joined: Fri Dec 28, 2018 1:41 pm
Location: Krakow, Poland
Has thanked: 8 times
Been thanked: 18 times

Limit of characters number in the input field

Unread post 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.
If you like nuBuilder, please leave a review on SourceForge
kev1n
nuBuilder Team
Posts: 4305
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 446 times
Contact:

Re: Limit of characters number in the input field

Unread post by kev1n »

Untested:

f

Code: Select all

unction limit_char(field, qty) {

    if ($('#' + field).val().length > qty) {
        $('#' + field).val($('#' + field).val().substr(0, qty));
    }
}
kev1n
nuBuilder Team
Posts: 4305
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 446 times
Contact:

Re: Limit of characters number in the input field

Unread post 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.
Last edited by kev1n on Tue Jun 11, 2019 2:45 pm, edited 1 time in total.
Janusz
nuBuilder Team
Posts: 506
Joined: Fri Dec 28, 2018 1:41 pm
Location: Krakow, Poland
Has thanked: 8 times
Been thanked: 18 times

Re: Limit of characters number in the input field

Unread post by Janusz »

Thanks a lot - works perfectly.
If you like nuBuilder, please leave a review on SourceForge
admin
Site Admin
Posts: 2815
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Limit of characters number in the input field

Unread post by admin »

.
Post Reply