1

I want to perform an action after the user typed in a textbox. I will be using onkeyup in the textbox.

i want a condition, that will allow the user to perform a task, only if the typed word is a dictionary word or a proper word.

Eg :

  • if the user types hello, then alert the word Hello.
  • if the user types helr, then alert that this is not a dictionary word.

HTML :

<input type="text" onkeyup="chk();"/>
<span id="indicate"></span>

Javascript :

function chk() {
  if(spellcheck()) {
     document.getElementById("indicate").innerHTML = "Correct Word";
  }
  else {
     document.getElementById("indicate").innerHTML = "Wrong Word";
  }
}

Please Help me defining the function spellcheck() which will return 1 or 0.

I want this to be performed in client side itself, using javascript.

Thanks in advance.

Prabakaran Raja
  • 720
  • 1
  • 11
  • 27

3 Answers3

1

Returning 1 and 0 is not good for JavaScript. Better return true and false from the function. Now, you can define your dictionary in form of array and then check if the input word is present in dictionary or not.

var dictionary = ['Hello', 'Welcome', 'Bye'];

function spellcheck(value) {
  var returnVal = false;
  var length = dictionary.length;
  for (var inc = length - 1; inc >= 0; inc--) {
    if(value.toUpperCase() === dictionary[inc].toUpperCase()) {
      returnVal = true;
      break;
    }
  }
  return returnVal;
}

You can see full example here http://jsbin.com/IgaPEBi/3/edit?html,js,output

Bikas
  • 2,709
  • 14
  • 32
  • How am i supposed to add all the dictionary words in a array variable? – Prabakaran Raja Oct 24 '13 at 09:30
  • 1
    See this http://stackoverflow.com/questions/11493594/javascript-list-of-english-words-for-a-game – Bikas Oct 24 '13 at 09:50
  • Selected as accepted answer then rejected. May I know what went wrong? – Bikas Nov 26 '13 at 07:50
  • I chose your answer as it worked good. I made loading all the dictionary words. but it made few issues like slowing down the app. So, i found that i should not load one. i have to access the dictionary words of the system – Prabakaran Raja Nov 26 '13 at 10:50
0

You can use the Typo.js dictionary

To use Typo, simply include the typo.js file in your extension's background page, and then initialize the dictionary like so:

var dictionary = new Typo("en_US");
var is_spelled_correctly = dictionary.check("hello");

or http://www.javascriptspellcheck.com/ for an auto suggestions box.

griffon vulture
  • 6,594
  • 6
  • 36
  • 57
0

Make a call to an API dictionary service, there are some opensource ones:

wiktionary

glosbe.com

dictionary-api

When the user finishes typing you can use a function to make an async call (AJAX with JS or Jquery) to the APIs endopoint; if the object in the response is not void or null or just states there are no words in the dictionary, your function can return 0, else 1.

Usually the endpoint address is something like URL/action/*word* , where action is what you want to know and word is the actual word you look for.

tuned
  • 1,085
  • 10
  • 14