Click here to Skip to main content
15,867,594 members
Articles / Web Development / ASP.NET

An ASP.NET Spell Checker for a Textbox

Rate me:
Please Sign up or sign in to vote.
4.09/5 (11 votes)
1 Jul 2007CPOL3 min read 113.7K   3.4K   47   25
An ASP.NET spell checker for text entered into a textbox.

Screenshot - checkspellingscreenshot.jpg

Introduction

I created my own blog at http://www.bid4binary.com and was finding that I was adding a lot of articles with spelling mistakes and then having to go back and modify these articles later (sometimes only after my wife or a friend pointed them out - ouch!). I really needed something that would check my spelling before posting the article so that I could just add it and be done with it.

Background

I did quite a bit of searching on the web and found many controls that would do this, but unfortunately, these were quite expensive (especially for a South African). Still others used Google APIs that seem to have been removed, or Microsoft Word APIs that are more suited for Windows applications and not web applications.

I then found a gem here on CodeProject by Vasantha Mohan (http://www.codeproject.com/asp/spell_check.asp) that describes how to use a dictionary file to check spelling in ASP. This was able to provide a good base for me to convert to ASP.NET and make some changes more suited for my project. I did not use his method to insert hard-returns as I am simply checking the text, not the formatting. In addition, I added an enhancement whereby the user can manually type an alternative word if no suitable suggestion was found by the spell checker, and modified the code so that the function only searches for alternatives beginning with the same letter as the word being checked - speeding things up significantly.

It's not perfect, but it works well - some further enhancements I'd like to see would be some kind of progress indicator so you know what it's doing while waiting, and the ability to add words to the dictionary file.

Using the code

The downloadable zip file contains five files:

  1. dict-large.txt - Contains a list of words to check against, place this in the same directory as the pages below.
  2. TestSpellCheck.aspx - This is the page you need to start with, it contains the textbox where you should enter the text to be checked for spelling errors.
  3. TestSpellCheck.aspx.vb - Nothing useful here, just a standard code-behind file.
  4. SpellSuggest.aspx - This is the page that displays the incorrect words and allows you to select an alternative or manually type an alternative.
  5. SpellSuggest.aspx.vb - Here's where the magic happens.

I am not going to show any code here as it is very well commented in the source code and it will be more useful to simply describe what it does. Feel free to contact me should you need any assistance.

In TestSpellCheck, we take the text that the user entered into the textbox, trim off any leading or trailing spaces, and then open a new window, passing this trimmed text as a URL parameter.

In SpellSuggest, we split the text into an array of individual words and check these against our dictionary file. For the current word, we only load each word from our dictionary beginning with the same letter. We then check that we have valid characters in the word to check, and if not, simply skip to the next; we then check if it's a number, and skip to the next if it is; next, we check if it's a valid word and skip if it is.

If by now we have not skipped, then our word is not in our dictionary, and we stop to allow the user to select an alternative from a list of suggestions, or manually type an alternative.

We also allow the user to cancel the check at any time. If they do this, we replace the part that they have already checked and leave the rest alone.

If they have reached the end of the checking, we show them a message, replace the original text, and close the SpellSuggest window.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Suggestionfix to make NEXT, STOP buttons proceed beyond first word Pin
JoeShields4-Dec-13 7:11
JoeShields4-Dec-13 7:11 
QuestionWant c# code for SpellCHecking Pin
rajender120318-Jul-12 18:52
rajender120318-Jul-12 18:52 
Questionurgent Pin
amr rabie17-Oct-11 10:24
amr rabie17-Oct-11 10:24 
GeneralDoes not work for some words.. Pin
AmMadhav3-Nov-09 10:45
AmMadhav3-Nov-09 10:45 
GeneralI am so sorry, but I'm unable to support the code anymore Pin
mikvos20-Aug-09 4:18
mikvos20-Aug-09 4:18 
GeneralHi Pin
pulokesh20-Aug-09 4:09
pulokesh20-Aug-09 4:09 
Is there any license attached to this code. Can I use it in my project.
GeneralRe: Hi Pin
mikvos20-Aug-09 4:14
mikvos20-Aug-09 4:14 
GeneralNext and Stop buttons don't work Pin
MLightsOut3-Aug-09 11:47
MLightsOut3-Aug-09 11:47 
GeneralRe: Next and Stop buttons don't work Pin
xwitch22-Oct-09 0:14
xwitch22-Oct-09 0:14 
QuestionWorks partially. Can someone please help me with this?????????????????????? Pin
somepi22-Apr-09 18:16
somepi22-Apr-09 18:16 
AnswerRe: Works partially. Can someone please help me with this?????????????????????? Pin
Ladybolt17-Aug-09 16:11
Ladybolt17-Aug-09 16:11 
GeneralIncorrect suggestions Pin
mayankagarwa;22-Apr-09 16:55
mayankagarwa;22-Apr-09 16:55 
GeneralIncorrect suggestions Pin
mayankagarwa;22-Apr-09 16:55
mayankagarwa;22-Apr-09 16:55 
GeneralMy vote of 1 Pin
Imparatorvolkan31-Mar-09 11:56
Imparatorvolkan31-Mar-09 11:56 
GeneralExcellent Pin
AmMadhav25-Mar-09 4:51
AmMadhav25-Mar-09 4:51 
QuestionHelp Pin
Joshua Paul Landry15-Mar-09 8:57
Joshua Paul Landry15-Mar-09 8:57 
AnswerRe: Help Pin
GB 195320-Jun-09 15:42
GB 195320-Jun-09 15:42 
QuestionC# Version availible? Pin
Sunil1286-Mar-09 4:02
Sunil1286-Mar-09 4:02 
GeneralSpelling Checker and Master page Pin
Member 452956426-Aug-08 5:27
Member 452956426-Aug-08 5:27 
GeneralGreat article, but single quoto did not work Pin
Tony Dong18-Feb-08 7:44
Tony Dong18-Feb-08 7:44 
GeneralAvoid popup Pin
koese12-Aug-07 19:32
koese12-Aug-07 19:32 
GeneralRe: Avoid popup Pin
mikvos12-Aug-07 19:59
mikvos12-Aug-07 19:59 
GeneralVery Gud job Pin
Mukesh_B10-Jul-07 20:58
Mukesh_B10-Jul-07 20:58 
GeneralSweet Pin
Curt Ophoven10-Jul-07 6:10
Curt Ophoven10-Jul-07 6:10 
GeneralRe: Sweet Pin
mikvos10-Jul-07 17:57
mikvos10-Jul-07 17:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.