This space is available to any ComponentArt employee to write about anything.

SpellCheck Client-side API

The SpellCheck control, like most others in Web.UI, includes a powerful client-side API. The philosophy behind SpellCheck’s design is that as little as possible should be baked right into the control when it comes to visual interface, and that custom user experiences should be made as easy as possible via a powerful and versatile client-side API.

Below are some examples.

To check some text and see how many errors there are, we can do the following:
 

var text = "This is some text with spleling misteaks in it."

function spellComplete(result)
{
alert('SpellCheck complete: ' + Spell1.get_numErrors() + ' errors found.');
}

Spell1.Check(text, spellComplete);


Continuing from the above example, this is how we can go through the errors and see what they are:
 

for(var i = 0; i < Spell1.get_numErrors(); i++)
{
alert('Error number ' + i + ' is: ' + Spell1.getError(i));
}


We can expand the loop above to show the user what some alternatives might be for each of those errors:
 

var currentError;
function suggestHandler(result)
{
alert('You typed\'' + currentError + '\' but maybe you meant to type one of these: ' + result);
}

for(var i = 0; i < Spell1.get_numErrors(); i++)
{
currentError = Spell1.getError(i);
Spell1.getSuggestions(currentError, suggestHandler);
}


We can also use the client-side API to change errors in the text right where they appear. For instance, we could alter the above to simply replace every misspelled word with the first suggestion returned by SpellCheck:
 

var currentErrorIndex;

function suggestHandler(result)
{
text = Spell1.Change(text, null, currentErrorIndex, result[0]);
}

for(var i = 0; i < Spell1.get_numErrors(); i++)
{
currentErrorIndex = i;
Spell1.getSuggestions(currentError, suggestHandler);
}


In the above example, we’ve been passing around the text variable, which we defined ourselves. When SpellCheck’s ControlToCheck property is set, null can be passed instead of text to any method that requires it – the control will get the relevant text itself, and interventions will be made right on the text in question, whether it’s in a form field or any other DOM element.

We hope you find this useful. Let us know what you think.

 

Share this post: email it! | bookmark it! | digg it! | reddit!

Posted by: Milos
Posted: Wednesday, November 07, 2007 10:55 AM


Comments

Dipal Bhavsar said:

Does Spell Ckech support multi lenguage support? Does it support for Swedish Language?
# September 9, 2008 12:13 AM

Magesh said:

When i use spellcheck control of asp.net 2.0, I am getting error "error loading callback data". can u help me?? (I need spellcheck control source code).
# November 19, 2008 11:51 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Blogs On This Site
Thoughts on web user interfaces and component development.
Ramblings of a web control developer.
Web.UI news and more
Next weeks guest: A dog and a baby dog!
I'm in your base, killing your dudes.
Absurdity is it's own message.
Musings of an ex Java developer
im in ur page, hackin ur codez
ComponentArt in the Community
... and the program ran happily ever after.

This Blog