Avatar of SueJStevens
SueJStevens
Flag for United States of America asked on

How can I validate length of text after form and data is loaded?

I have developed simple form that users will input data.  I've written validation scripts to show the user how many characters remain available.  The script works great so long as the form that is loaded is expecting new data entry.  Now I'd like to use that same form for users to edit previously entered data.  What do I need to change in this script so the length of data loaded is taken into account when the window is loaded?

My form has an input
<input type="text" name="fieldname" id="fieldname"       maxlength="20" placeholder="Field Name"
value="<?php echo $row['FieldName']; ?>">

and my form has a feedback message
<p style="text-align: right" id="feedback_fieldname"></p>

Here is my validation script:
<!--Inform User Remaining Length of input fields-->
<script>
window.onload = function() {

    var text_max_fieldname = 20;
    $('#feedback_fieldname').html(text_max_fieldname + ' characters remain ');

    $('#fieldname').keyup(function() {
        var text_length_fieldname = $('#fieldname').val().length;
        var text_remaining_fieldname = text_max_fieldname - text_length_fieldname;

        $('#feedback_fieldname').html(text_remaining_fieldname + ' characters remain ');
    });

}
</script>
JavaScriptHTMLjQuery

Avatar of undefined
Last Comment
Brian Tao

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Brian Tao

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SueJStevens

ASKER
Brian,
Thank you!  I really appreciate the feedback.  Because I am a newbie, I especially appreciate that you created a model via jsfiddle for me.  Closing this question and awarding points.
SueJStevens

ASKER
I appreciate the quick response and thorough answer.
Brian Tao

Glad that helps.  Thanks for the points.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy