...

View Full Version : Text limit using txtcopy function?



longman
03-11-2009, 05:11 AM
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.



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

milanchheda
03-11-2009, 05:31 AM
<!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", Times, serif;
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>

longman
03-11-2009, 06:50 AM
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!

Philip M
03-11-2009, 09:56 AM
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:-


<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

longman
03-12-2009, 05:20 AM
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:




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!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum