本篇來講講怎麼用Javascript去驗證Text中的字數
其實也沒什麼祕訣
只要利用 .length 的屬性就可以算出字數
直接看下面的抓圖吧

上圖為計算字數的 js function
關鍵在於用 return 的 true/false 值來控制 button 會不會 submit

上圖為 Button 控制項的屬性
OnClick 事件是承襲 上篇 的 Button 事件
不過這邊要多加上個 OnClientClick 事件 return CheckWordCount()
這是控制 Button 會不會 submit 的關鍵

送出驗證事件

驗證失敗事件
由上圖可得知 上一篇 坎尼提到斷行的問題
Javascript 裡會將斷行視為一個字 (坎尼認為是\n)
兩個方式都可用來驗證目前字數
不過需注意兩個方法對文字字數的處理結果
沒有留言:
張貼留言