Change the color of the word in the textbox when mispelled.

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
icesha
Forum Newbie
Posts: 11
Joined: Tue May 01, 2007 3:15 am

Change the color of the word in the textbox when mispelled.

Post by icesha »

Hi! I need help regarding a code i have been developing, i want to change the color of the generated output text when it is detected as wrong spelling. I can already check the spelling. I want it to look like the MSWORD where wrong spellings are underlines, as red line. Just like in this textbox. please i need help.

all i can do with this code is output it in a different page and change the color of the mispelled word. What i want is to underline or change the color of the mispelled word in the same page and same textbox.
here is my code. please. need help.

Code: Select all

<?php

include("ecsel_connectdb.php");

$var=$_POST['textarea'];

$var= trim($var,"\t\r\n");
$mypar=explode('.',$var);
//sentence


foreach($mypar as $b)
{
$b=trim($b, "\t\r\n "); 
echo "$b - This is a sentence";
echo "<br/>";
echo "<br/>";
}//end first for

$var=trim(str_replace('.','',$var));
$myarray=explode(' ',$var);
$pspell_link = pspell_new("en");

foreach($myarray as $a)
{
 	 $var=trim(str_replace('.','',$var));
	 $a=trim($a, "\t\r\n ");  	 	 	 	   
   		 $pspell_link = pspell_new("en");
		 if (pspell_check($pspell_link, $a))
   			{	
				echo "$a - This is a valid spelling";
				echo "<br/>";  
				echo "<br/>"; 	
     		} //end if
  		 else 
			{		
				/*echo "$a - Sorry, wrong spelling";
				echo "<br/>"; 
				echo "<br/>"; 
  */
              /* echo $var;
			   echo "<br/>"; */
			   
			   
			   echo "<font size=\"3\" face=\"Segoe UI\" color=\"red\"> $a <b><br /><br /><br><br> </font>";	
  				
               $wrong=$wrong +1;
                  if (!pspell_check($pspell_link, $a)) 
					   {
       				 		$suggestions = pspell_suggest($pspell_link,$a);
    						foreach ($suggestions as $suggestion) 
							{
       							
																
								echo "Possible spelling: $suggestion<br />";
								//echo "<br/>";  
							}//end inner for
					  }//end inner if
			}//end  elsehind
	

}//end for		
	
	
	
?>
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

You can't style specific parts of the contents in a textarea/input(type=text) with CSS/HTML styling.
There are ways to do this, but it will require Javascript and I'm not the one to explain it (don't have much knowledge in the area).
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

You could do with using AJAX and php, but you may require a MySQL dictionary to query agains misspelled word onchange

<textarea name="textarea" onchange="checkSpelling(this.value)">Microsof i$ blah etc</textarea>

Read up on AJAX at http://www.w3schools.com
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

First I don' t know why are trying to do this the majority on current generation browsers have word spelling checker except IE 7. You' ll need a huge database for this.
Read some tutorials on AJAX, PHP and MySQL then come back with questions. It' s simple the server side part but on the client side (JavaScript) :( (I don' t have a clue how you will check each word that the user writes you' ll need a function that checks that user has wrote a word).
UrButtFullOfArr0ws
Forum Commoner
Posts: 64
Joined: Wed Feb 21, 2007 11:42 am
Location: Up a tree >.>"

Post by UrButtFullOfArr0ws »

To ianhull: onchange doesn't seem to be SO dynamic, and i''d say onkeyup would be a better choice. And so that it doesnt check from the first letter you can add a middle function of the kind:

Code: Select all

function CheckLength(){
if (length >= 3){
checkSpelling(value);
}
anyway you get the idea :)
Post Reply