Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2007
    Posts
    98
    Thanks
    24
    Thanked 4 Times in 4 Posts

    Text limit using txtcopy function?

    Hi,

    This is probably fairly basic for the gurus, but it's stumping me right now.

    I'm using a web form to gather input data. When the submit button is pressed, the input data is placed into a formatted document and copied to clipboard so it can be pasted into another document window (it has to be done this way for security reasons).

    I'm using the following code to format and copy the data.

    Code:
       document.getElementById('txtCopy').value = strDoc
       document.getElementById('txtCopy').select()
       txtCopied = document.getElementById('txtCopy').createTextRange()
       txtCopied.execCommand("copy")
    Q: Is it possible that once the doc is formatted that the total number of characters can be counted (including spaces) and an alert show if the total number of characters reaches a defined limit?

    I basically have to ensure that the alert shows if the output doc will be 3900 characters or greater.

    Cheers and let me know if any additional code is needed.

    A

  • #2
    New Coder
    Join Date
    Feb 2009
    Posts
    38
    Thanks
    0
    Thanked 1 Time in 1 Post
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <
    html xmlns="http://www.w3.org/1999/xhtml">
    <
    head>
    <
    meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <
    title>Untitled Document</title>
    <
    style type="text/css">
    body{
        
    font-family:"Lucida Grande""Lucida Sans Unicode"Verdana;
        
    font-size:12px;
    }
    .
    counter{
        
    font-family:Georgia"Times New Roman"Timesserif;
        
    font-size:24px;
        
    font-weight:bold;
        
    color:#999999;
    }
    input{
        
    font-family:"Lucida Grande""Lucida Sans Unicode"Verdana;
        
    font-size:13px;
        
    color:#999999;
    }
    label{
        
    font-family:"Lucida Grande""Lucida Sans Unicode"Verdana;
        
    font-size:11px;
        
    display:block;
        
    color:#333333;
    }
    </
    style>
    <
    script type="text/javascript">
    function 
    countChars(idElement){
        
    max_chars         20;
        
    counter            document.getElementById(idElement);
        
    field            document.getElementById('text').value;  
        
    field_length    field.length
        remaining_chars    
    max_chars-field_length;
            if(
    remaining_chars<=5){
                
    counter.style.color="#CC0000";
            }

        
    // Update the counter on the page
        
    counter.innerHTML remaining_chars;
    }
    </script>
    </head>

    <body>
    <label for="text">Write something here</label>
    <input type="text" id="text" onKeyUp="javascript:countChars('counter_number')" maxlength="20"/> 
    <span id="counter_number" class="counter">20</span>  remaining chars
    </body>
    </html> 

  • #3
    New Coder
    Join Date
    Mar 2007
    Posts
    98
    Thanks
    24
    Thanked 4 Times in 4 Posts
    Thanks for the reply!!

    Perhaps I need to explain myself better. Once all the fields in the webform are completed, the "create document" button is pressed.

    This then formats the output document and copies it to the clipboard.

    I need to validate the total number of characters in the output document to less than 3900 characters. I have limited the individual text boxes as much as possible, but several fields require free input and cannot be limited.

    Thanks again!

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,922
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    If I understand you correctly, the total number of characters in all fields combined cannot exceed 3900.

    So simply add the lengths of all the fields (e.g. totlen = field1.length + field2.length .....) and alert if over 3900 (request user to reduce input content).

    or here

    var totlen = strDoc.length;
    if (totlen > 3900) {
    alert ("Total of all fields too long!);
    return false;
    }
    document.getElementById('txtCopy').value = strDoc

    You may find this of use as well:-

    Code:
    <script type = "text/javascript">
    
    function countChar(ID){
    var nChar = ID.value.match(/[^\r\n]/g);
    var nCRLF = ID.value.match(/\r\n/g);
    if (nChar != null) {nChar = nChar.length}; else {nChar = 0}
    if (nCRLF != null) {nCRLF = nCRLF.length}; else {nCRLF = 0}
    nChar = nChar+nCRLF;
    alert (nChar);
    }
    
    </script>
    
    <h4>Count Characters in a Text Area, including spaces and CR/LF</h4>
    <textarea cols=32 rows=10 id='area1'>The rain in Spain falls mainly on the plain.
    </textarea>
    <br><br>
    <input type=button value='Count Characters' onClick="countChar(area1)">

    Brevior saltare cum deformibus mulieribus est vita - Life is too short to dance with ugly women
    Last edited by Philip M; 03-11-2009 at 09:48 AM. Reason: Typo (missing / in </script>

  • #5
    New Coder
    Join Date
    Mar 2007
    Posts
    98
    Thanks
    24
    Thanked 4 Times in 4 Posts
    We're getting there!

    Basically, I have a html web form. I then use the "function createDocument()" to build a word doc, formatted the way I need it with the input data.

    I need the character count and alert to work on the created output document (basically everything contained in a strDOC line).

    an example of the createDocument is as follows:

    Code:
          
    function createDocument()
    {
    
    strDoc = "Document goes here.\n"
    strDoc += "\ \n"
    strDoc += "I need to be able to count all this.\n"
    strDoc += "Name:\t" + document.getElementById('txtname').value + "\n"
    strDoc += "Address:" + document.getElementById('txtempaddress1').value + "\n"
         
    document.getElementById('txtCopy').value = strDoc
    document.getElementById('txtCopy').select()
    txtCopied = document.getElementById('txtCopy').createTextRange()
    txtCopied.execCommand("copy")
      
    successMsg = "Document has been copied to clipboard and can now be pasted into Word."
    
    alert("Letter has been formatted and copied to clipboard and can now be pasted into word.")
    
    resetq = confirm("Click OK to clear the form.")
    if (resetq)  
      {
       document.forms["frmletter"].reset()
       scroll(0,0)
       document.getElementById('txtdate').focus()
      }
    }
    Thanks for the replies!


  •  

    Posting Permissions

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