...

View Full Version : multiline textbox validation behaves differently in IE and FF



write2atif
05-12-2009, 08:12 AM
hi all , i want that user can not enter text in multiline textbox more than specific characters but it behaves differently in IE and FF. In Internet explorer when i press enter key it takes two character whereas in firefox when i press enter it takes only one character. so this way user can enter more data in FF which is not correct. how to fix that behavior so that both browsers give same result. My javascript code is given below

<HTML>
<HEAD>
<TITLE> Javascript Character Count by WebSewak.com </TITLE>
<script language="Javascript">
function counterUpdate(opt_countedTextBox, opt_countBody, opt_maxSize) {
var countedTextBox = opt_countedTextBox ? opt_countedTextBox : "counttxt";
var countBody = opt_countBody ? opt_countBody : "countBody";
var maxSize = opt_maxSize ? opt_maxSize : 1024;

var field = document.getElementById(countedTextBox);

if (field && field.value.length >= maxSize) {
field.value = field.value.substring(0, maxSize);
}
var txtField = document.getElementById(countBody);
if (txtField) {
txtField.innerHTML = field.value.length;
}
}
</script>
</HEAD>

<BODY>
<textarea id="counttxt" name="counttxt" rows="10" onkeyup="counterUpdate('counttxt', 'countBody','30');"></textarea>

You typed <B><span id="countBody">0</span></b> characters
Max. Length : 30 Chars
</BODY>
</HTML>

Philip M
05-12-2009, 09:11 AM
Try this:-


<html>
<head>

<script type = "text/javascript">

function countChar(ID){
nChar = ID.value.match(/[^\r\n]/g);
nCRLF = ID.value.match(/\n/g);
if (nChar != null){nChar = nChar.length};
else{nChar = 0}
if (nCRLF != null){nCRLF = nCRLF.length};
else{nCRLF = 0}
nChar = nChar+nCRLF;
alert("You typed " + nChar + " characters");
}

</script>
</head>

<body>
<center>
<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)">
</center>
<br>

</body>
</html>


"It [a penalty decison] is one that could have gone either way - and it did!" Football commentator.

write2atif
05-12-2009, 09:35 AM
hi philips,

i tried ur code but the problem is same in firefox it gives that 44 characters are typed whereas in IE result is 45 characters.

Philip M
05-12-2009, 10:13 AM
hi philips,

i tried ur code but the problem is same in firefox it gives that 44 characters are typed whereas in IE result is 45 characters.


Change this line:-

nCRLF = ID.value.match(/\r\n/g);

to

nCRLF = ID.value.match(/\n/g);

write2atif
05-12-2009, 11:32 AM
hi philips,
thanx for ur help. u really solved my problem .can u plz explain the logic u used and the reular expressions. thanx again

barkermn01
05-12-2009, 11:37 AM
i know the problem,

IE allows a space as a char,

FF on the other hand you have a space it is not there untill you have a char after the space

But i bilive IE auto puts a Space on the end of a value feild not sure about that though but i think its works like below

E.G

IE - "Hello " this is 6
FF - "Hello " this is 5

But if i put a char after the space
IE - "Hello X" this is 7
FF - "Hello X" this is 7

Philip M
05-12-2009, 12:22 PM
i know the problem,

IE allows a space as a char,

FF on the other hand you have a space it is not there untill you have a char after the space

But i bilive IE auto puts a Space on the end of a value feild not sure about that though but i think its works like below

E.G

IE - "Hello " this is 6
FF - "Hello " this is 5

But if i put a char after the space
IE - "Hello X" this is 7
FF - "Hello X" this is 7

No, that is not right.


It is simply that in IE a newline/carriage return sequence is expressed as \r\n, and in Firefox \n only.

You could get the same result by adding the line:-

ID.value = ID.value.replace(/\r\n/g,'\n');

barkermn01
05-12-2009, 12:52 PM
Got it just a note do it the other way replace \n with \r\n this way it is compatible for both window and linux as \r\n works with linux but \n dose not with windows try opening a file created with \n in notepad it just buggers up,

computerzworld
10-28-2009, 06:51 AM
Hello,
I am facing the same problem. Whenever I press enter key in firefox it considers it as one character while in IE,Opera it considers it two characters. So is there any way to make them identical? I mean in all browsers it should consider two characters for enter key press. Also in php script I have counted the characters and in that also it considers enter as two characters. So if it considers enter as one character then it will allow more characters to be entered in firefox and it should not happen. Please help me. Thanks in advance.

Philip M
10-28-2009, 10:19 AM
I don't understand. AFAIK the script above counts correctly in IE and in Firefox. Is this the script you are using?

However, unless corrected Firefox throws an error with these lines (delete the semi-colon):-

if (nChar != null){nChar = nChar.length} ;
else{nChar = 0}
if (nCRLF != null){nCRLF = nCRLF.length} ;

If you are talking about some other PHP script you should post in the appropriate forum.

computerzworld
10-28-2009, 01:01 PM
yes i tried the script but it considers enter as one character while I want it to consider as two characters so this is the point where i got stuck.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum