65.9K
CodeProject 正在变化。 阅读更多。
Home

拼写检查文本区域

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.83/5 (6投票s)

2004年7月1日

2分钟阅读

viewsIcon

108733

downloadIcon

1748

文本区域拼写检查。

Sample Image - spell_check.gif

引言

创建这个小型 Web 应用程序一直是我梦寐以求的事情。我搜索了网络,发现只有付费支持与拼写检查相关的 Web 应用程序,除了 Sam @ KIRCHMEIER.ORG 之外,他实际上启发了我开发这个拼写检查,它不仅允许验证一个单词,而且允许验证整个段落。

默认文档设置

如果您的 IIS 没有设置为打开 *index.asp* 作为默认文档,您要么必须键入完整的 URL,包括 *../index.asp*,要么将其包含到默认文档中。

数据库用户

如果您更改此脚本以使用数据库,您应该注意以下几点。排序顺序对于此脚本非常重要;如果单词列表没有正确排序,脚本将无法正常工作或根本无法工作。排序顺序在数据库之间会有所不同,有时 VBScript 的字符串比较与您的数据库产生的排序顺序不一致。例如,单词:standards 和 standard's,可能会以该顺序出现在已排序的结果集中。但是 VBScript 规定 standard's < standards,因此 standard's 必须首先出现在单词列表中才能被 SpellCheck 函数找到。

已更新为 MAV

代码的作用简介

  1. 用著名的 MS office 换行符 "¶" 分割句子。
  2. 检查用户是否未提交空白文本字段。
  3. 从传递的字符串中删除前导和尾随空格。
  4. 打开一个新窗口来发布更改后的文本。
  5. 将单词从传递的字符串拆分为虚拟 3D 数组。
  6. 将单词解析为 *spell.asp*。
  7. *Spell.asp* - Soundex 函数。
  8. 显示拼写检查后的文本。

用著名的 MS office 换行符 "¶" 分割句子。

每当用户点击回车键时,字符串 " ¶ " 连同换行符一起附加到文本中。

function DisplayEvent()
{
 if (event.keyCode == 13)
 {
  myMessage  = window.document.F1.T1.value+" "+"¶"+" ";
  window.document.F1.T1.value=myMessage
 }
}

检查用户是否未提交空白文本字段。

验证是 "良好编程技术" 中的一个强制性要点。

此处的代码验证文本字段是否为空白

if (trim(document.F1.T1.value)=="")
{
 alert("Enter Text");
 document.F1.T1.focus();
 return false;
}

如果为空白,它会停止进一步处理,并提醒用户输入文本。

从传递的字符串中删除前导和尾随空格。

从传递的字符串中删除前导和尾随空格。 还会删除连续空格并将其替换为一个空格。

如果传入的不是字符串(null、自定义对象等),则返回输入。

function trim(inputString)
{
 if (typeof inputString != "string") { return inputString; }
 var retValue = inputString;
 var ch = retValue.substring(0, 1);
 while (ch == " ")
  { // Check for spaces at the beginning of the string

  retValue = retValue.substring(1, retValue.length);
  ch = retValue.substring(0, 1);
 }
 ch = retValue.substring(retValue.length-1, retValue.length);
 while (ch == " ")
 { // Check for spaces at the end of the string

  retValue = retValue.substring(0, retValue.length-1);
  ch = retValue.substring(retValue.length-1, retValue.length);
 }
 while (retValue.indexOf("  ") != -1)
 { 
 // Note that there are two spaces in the string

 // - look for multiple spaces within the string

  retValue = retValue.substring(0, 
    retValue.indexOf("  ")) + 
    retValue.substring(retValue.indexOf("  ")+1, 

 retValue.length);
 // Again, there are two spaces in each of the strings

 }
 return retValue; // Return the trimmed string back to the user

}

打开一个新窗口来发布更改后的文本。

val=window.document.F1.T1.value
var winl = (screen.width - w) /2; 
// Synchronize to the current screen resolution

var wint = (screen.height - h) / 2; 
// Synchronize to the current screen resolution

mypage="spellcheck.asp?T1="+val
winprops = 'height='+h+',width='+w+',
           top='+wint+',left='+winl+',
           scrollbars='+scroll+',resizable'
win = window.open(mypage, myname, winprops)
return false;

将单词从传递的字符串拆分为虚拟 3D 数组。

a1 = Request("T1")
a1=Trim(a1)
a=split(a1," ") // Delimiter here is blank space

For i=0 to UBound(a)-1 
// Returns the largest available subscript
// for the indicated dimension of an array
 b=b++a(i+1)+" "
Next

Response.Cookies("myword")=b

将单词解析为 spell.asp。

if PrepForSpellCheck(strHaha) then 
// Validates whether the alrtered string has alpha variables only
 Response.Cookies("final")=Request.Cookies("final")+" "+strHaha
 Response.Redirect("spellcheck1.asp")
end if

Dim strHaha
Dim strWord

if a(0) & "" <> "" then
 LoadDictArray
 strHaha = a(0)

 if SpellCheck(strHaha) then
  Response.Cookies("final")=Request.Cookies("final")+" "+strHaha
  Response.Redirect("spellcheck1.asp")
 end if

 if IsNumeric(a(0)) then
  Session("final")=Session("final")+" "+a(0)
  Session("final")=Trim(Session("final"))
  Response.Redirect("spellcheck1.asp")
 end if

 k=1

 for each strWord in Suggest(strHaha)
  k=k+1
 next

 if k=1 then
  Response.Cookies("final")=Request.Cookies("final")+" "+a(0)
  Response.Redirect("spellcheck1.asp")
 end if

 if k>8 then
  k=6
 end if

Spell.asp - Soundex 函数。

我建议您参考 此站点,以便您可以更好地了解 Soundex 函数。

显示拼写检查后的文本。

a=Request.Cookies("final")
a=Replace(a,"¶","<br>") 
// Returns a string in which a specified
// substring (¶) has been replaced with
// another substring (<br>)
Response.Write a
© . All rights reserved.