954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Inform the number of characters inputted upon typing

<html>
<head>
		<script type="text/javascript">
				function check()
				{
					var val = document.myForm.content.value;
					if (val.length <= 10)
					    {
					       document.getElementById('numC').value = val.length;
					       //out.value = val.length;
					       
					    }
					 else
						document.write("Please enter the text in  range of 10 characters!");
					return false;	
				} 
		</script>
</head>
<body> 
       <form name="myForm">
       Type something here:
       <input type"textbox" value="" name="content"  onchange ="check()">
       <br/><br/>
       Lenght of string you enter:<input type="text" id="numC">
       </form>
</body>
</html>

Here is my code with the requirement: when I type text into the first input cell, the number of characters will be appear in the next cell, or if the number of character is greater than 10, the inform will be appear.
But I don't know how the inform will appear immediately if the tenth character is typed!
Please help me!
Thank you!

thebluestar
Junior Poster in Training
55 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

>> Line 13 - You forgot to add the {} at the else. You should always use an if...else the following:

if (...condition...) {

} else {

}


>> Line 14 - Never use a document.write() in a function you call when the page is already loaded. Use a custom message box or use alert("Message");


>> Line 22 - You should use the event onkeypress="check()" instead of onchange="check()"


~G

Graphix
Posting Pro in Training
432 posts since Aug 2009
Reputation Points: 82
Solved Threads: 74
 

I always prefer, countdown code, similar coding, but, characters remaining

almostbob
Posting Sensei
3,149 posts since Jan 2009
Reputation Points: 571
Solved Threads: 376
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You