Closed Thread
Results 1 to 3 of 3

Thread: Form: Textarea (restrict length)

  1. #1
    Barn Legend Rebelle will become famous soon enough Rebelle's Avatar
    Join Date
    Mar 2008
    Posts
    1,522
    Rep Power
    5

    Form: Textarea (restrict length)

    Hi All,

    I have a form that when the user tries to submit the form it runs function onSubmitForm..
    it checks to make sure required fields are not blank or like email field is valid email address..etc

    with one of my fields i used example like below for text field to make sure no more than 9 characters can be entered and it works fine....

    Code:
    formDOMObj.PartNo.value.length >='10'
    alert("Enter 9 digits or less!");
           formDOMObj.PartNo.focus();
           return false;
    
    But I have a textarea field (Comments) that is limited to 300 chars (sql table)....so if i go over that i get a sql error when the form is submitted...well, i thought maybe if i set the .length to >=301 then
    it may work like example above but it didn't work (no alert and just sql error)....maybe because its textarea?

    I found this nice example even though I would be happy with just an alert saying You've put it more than 300 characters,
    please revise.....should this be the way to go?

    Javascript Dynamic Text Area Counter

    Suggestions?

  2. #2
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Try using this method instead:-
    Code:
    if(document.getElementById('yourtextareaID').value.length>300){
    alert(document.getElementById('yourtextareaID').value.length+' is too many letters. You may only enter 300');
    return false;
    }else{
    return true;
    }
    

  3. #3
    Barn Legend Rebelle will become famous soon enough Rebelle's Avatar
    Join Date
    Mar 2008
    Posts
    1,522
    Rep Power
    5

    Thanks RR, I couldn't get it to work for me, tried many times, it's probably just me.......it kept bypassing the alert and just kept getting the ugly sql error....

    I went ahead and went with the second technique listed on link below: but added an alert when tries to type more than 300 chars....

    like so:
    Code:
    function limitText(limitField, limitNum) {
    	//next two lines are for alerting user than input has been truncated
    	if (limitField.value.length > limitNum + 1)
    	alert('Only 300 characters allowed - Your input has been truncated!');
        if (limitField.value.length > limitNum) {
            limitField.value = limitField.value.substring(0, limitNum);
        } 
    }
    
    with:
    Code:
    <TEXTAREA NAME="Comments" ROWS="5" COLS="30" onKeyUp="limitText(this,300);"></TEXTAREA>
    
    Limit a textarea to a maximum length - Real's Javascript How-to

Closed Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO