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 11 of 11
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    multiline textbox validation behaves differently in IE and FF

    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>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,992
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Try this:-

    Code:
    <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.
    Last edited by Philip M; 05-12-2009 at 10:03 AM. Reason: Fixed Firefox bug as below

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,992
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Quote Originally Posted by write2atif View Post
    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);
    Last edited by Philip M; 05-12-2009 at 09:57 AM.

  • Users who have thanked Philip M for this post:

    write2atif (05-12-2009)

  • #5
    New to the CF scene
    Join Date
    May 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #6
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts
    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
    Working towards a Internet where we don't have website just browser applications Kill the Hyper-link and say hello to 3D Games in the browser :)

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,992
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Quote Originally Posted by barkermn01 View Post
    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');

  • #8
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts
    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,
    Working towards a Internet where we don't have website just browser applications Kill the Hyper-link and say hello to 3D Games in the browser :)

  • #9
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,992
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    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.
    Last edited by Philip M; 10-28-2009 at 09:31 AM.

  • #11
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

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