1、在文本框中只接受“0-9”這10字符。而且年月日分隔符會自動生成。 
2、vb的年份為100-9999,本人限在1000-2999,不夠用嗎?如果真得不夠用,你可以自己改造加以控制。或許你會問:為什么年份不用兩位數來表示? 
我的觀點: 
1、兩位數的年份格式不直觀。 
2、如果碰到要記載出生年月日信息時,很可能會很難辦。 
3、月份只能從01-12,而且,當輸入3-9時,系統會自動默認為03-09。 
4、日期如果輸入4-9時系統也會自動默認為04-09。還有: 
a:當月份為1、3、5、7、8、10、12時,日期不能超過31 
b:當月份為4、6、9、11時,日期不能超過30 
c:當月份為2時且為閏年時,日期不能超過29 
d:當月份為2時且為非閏年,日期不能超過28 
有了以上的“優點”,只要輸入年月日完畢,就會確保它是一個合法的日期表達式。朋友,趕快把下面那段程序粘貼到vb工程中(當然,你也許要改變text1為其它的文本框編號如:text3)去試一下吧,相信,你會立即愛上她,直到永遠...... 
private sub text1_change() 
dim a, b, c as string 
--------------------------------------------------------------------------- 
年份輸入的控制 
--------------------------------------------------------------------------- 
限制第一位必須為"1"或"2" ,也就說年份必須在1000-2999之間,夠用吧! 
if len(text1.text) = 1 then 
if left((text1.text), 1) <> "1" and left((text1.text), 1) <> "2" then 
text1.text = "" 
end if 
end if 
限制第二、三、四位必須為“1、2、3、4、5、6、7、8、9、0” 
if len(text1.text) = 2 or len(text1.text) = 3 or len(text1.text) = 4 then 
if right((text1.text), 1) <> "0" and right((text1.text), 1) <> "1" and _ 
right((text1.text), 1) <> "2" and right((text1.text), 1) <> "3" and _ 
right((text1.text), 1) <> "4" and right((text1.text), 1) <> "5" and _ 
right((text1.text), 1) <> "6" and right((text1.text), 1) <> "7" and _ 
right((text1.text), 1) <> "8" and right((text1.text), 1) <> "9" then 
text1.text = left((text1.text), len(text1.text) - 1) 
text1.selstart = len(text1.text) 
end if 
end if 
if len(text1.text) = 4 then 
text1.text = text1.text + "-" 
text1.selstart = len(text1.text) 
end if 當年份正確輸入后就自動加上的“-”分隔符 
--------------------------------------------------------------------------- 
月份輸入的控制 
--------------------------------------------------------------------------- 
if len(text1.text) = 6 then 
if right((text1.text), 1) <> "0" and right((text1.text), 1) <> "1" then 
if right((text1.text), 1) = "2" or right((text1.text), 1) = "3" or _ 
right((text1.text), 1) = "4" or right((text1.text), 1) = "5" or _ 
right((text1.text), 1) = "6" or right((text1.text), 1) = "7" or _ 
right((text1.text), 1) = "8" or right((text1.text), 1) = "9" then 
a = right((text1.text), 1) 
text1.text = left((text1.text), 5) + "0" + a + "-" 
如果這樣,那下面一段if len(text1.text)=7的判斷自然就自動跳過去了。 
text1.selstart = len(text1.text) 
else 限制只能輸入“0-9” 
text1.text = left((text1.text), len(text1.text) - 1) 
text1.selstart = len(text1.text) 
end if 
end if 
end if 
if len(text1.text) = 7 then 
if left(right(text1.text, 2), 1) = "0" then 如果月份第一位為“0” 
if right((text1.text), 1) <> "1" and right((text1.text), 1) <> "2" and _ 
right((text1.text), 1) <> "3" and right((text1.text), 1) <> "4" and _ 
right((text1.text), 1) <> "5" and right((text1.text), 1) <> "6" and _ 
right((text1.text), 1) <> "7" and right((text1.text), 1) <> "8" and _ 
right((text1.text), 1) <> "9" then 
text1.text = left((text1.text), len(text1.text) - 1) 
text1.selstart = len(text1.text) 
else 
text1.text = text1.text + "-" 當月份輸入正確后自動加一個“-”分隔符 
text1.selstart = len(text1.text) 
exit sub 少不了!如果少,那當月份為“01”時,緊接的if...end if就 
成立,這樣會在這里出現死循環,而出現溢出堆棧空間的錯誤! 
注:本程序好幾個地方都可以用上exit sub,要加你自己補上吧! 
end if 
end if 
if left(right((text1.text), 2), 1) = "1" then 如果月份第一位為“1” 
if right((text1.text), 1) <> "0" and right((text1.text), 1) <> "1" and _ 
right((text1.text), 1) <> "2" then 
text1.text = left((text1.text), len(text1.text) - 1) 
text1.selstart = len(text1.text) 
else 
text1.text = text1.text + "-" 當月份輸入正確后自動加一個“-”分隔符 
text1.selstart = len(text1.text) 
end if 
end if 
end if 
--------------------------------------------------------------------------- 
日期輸入的控制。 
--------------------------------------------------------------------------- 
if len(text1.text) = 9 then 
if right((text1.text), 1) <> "0" and right((text1.text), 1) <> "1" and _ 
right((text1.text), 1) <> "2" and right((text1.text), 1) <> "3" then 
if right((text1.text), 1) = "4" or right((text1.text), 1) = "5" or _ 
right((text1.text), 1) = "6" or right((text1.text), 1) = "7" or _ 
right((text1.text), 1) = "8" or right((text1.text), 1) = "9" then 
a = right((text1.text), 1) 
text1.text = left((text1.text), 8) + "0" + a 
text1.selstart = len(text1.text) 
exit sub 
日期小于10時下面字符長度為10的判斷當然是正確的。讓它執行又如何? 
else 
text1.text = left((text1.text), len(text1.text) - 1) 
text1.selstart = len(text1.text) 
end if 
end if 
end if 
當要修改日期的最后一位時的控制。 
if len(text1.text) = 10 then 
b = left(right(text1.text, 5), 2) 取月份值,用于下面的日期正確性判斷! 
c = left(text1.text, 4) 取年份值,用于下面的日期正確性判斷! 
if right((text1.text), 1) <> "0" and right((text1.text), 1) <> "1" and _ 
right((text1.text), 1) <> "2" and right((text1.text), 1) <> "3" and _ 
right((text1.text), 1) <> "4" and right((text1.text), 1) <> "5" and _ 
right((text1.text), 1) <> "6" and right((text1.text), 1) <> "7" and _ 
right((text1.text), 1) <> "8" and right((text1.text), 1) <> "9" then 
text1.text = left((text1.text), len(text1.text) - 1) 
text1.selstart = len(text1.text) 
end if 限制非法字符的輸入。 
if right(text1.text, 2) = "00" then 
text1.text = left((text1.text), len(text1.text) - 2) 
text1.selstart = len(text1.text) 
end if 限制日期不能為0 
if (b = "01" and val(right(text1.text, 2)) > 31) or _ 
(b = "03" and val(right(text1.text, 2)) > 31) or _ 
(b = "05" and val(right(text1.text, 2)) > 31) or _ 
(b = "07" and val(right(text1.text, 2)) > 31) or _ 
(b = "08" and val(right(text1.text, 2)) > 31) or _ 
(b = "10" and val(right(text1.text, 2)) > 31) or _ 
(b = "12" and val(right(text1.text, 2)) > 31) then 
text1.text = left((text1.text), len(text1.text) - 2) 
text1.selstart = len(text1.text) 
end if 當月份為大月份時日期不能大于31。 
if (b = "04" and val(right(text1.text, 2)) > 30) or _ 
(b = "06" and val(right(text1.text, 2)) > 30) or _ 
(b = "09" and val(right(text1.text, 2)) > 30) or _ 
(b = "11" and val(right(text1.text, 2)) > 30) then 
text1.text = left((text1.text), len(text1.text) - 2) 
text1.selstart = len(text1.text) 
end if 當月份為小月份時日期不能大于30。 
if b = "02" then 
if val(c) mod 4 <> 0 and val(right(text1.text, 2)) > 28 then 
text1.text = left((text1.text), len(text1.text) - 2) 
text1.selstart = len(text1.text) 
end if 非閏年日期不得超過28。 
if val(c) mod 4 = 0 and val(right(text1.text, 2)) > 29 then 
text1.text = left((text1.text), len(text1.text) - 2) 
text1.selstart = len(text1.text) 
end if 閏年日期不得超過29。 
end if 當月份為2時的日期正確性判斷! 
end if 
--------------------------------------------------------------------------- 
當年月日輸入后就不再接受其它字符了。方法如下: 
--------------------------------------------------------------------------- 
第一種方法: 
在text1的屬性窗口中設maxlength=10 
第二種方法: 
text2.setfocus 即在適當的地方設一個跳轉語句使下一個對象得到焦點。 
第三種方法: 
if len(text1.text) = 11 then 
text1.text = left((text1.text), len(text1.text) - 1) 
text1.selstart = len(text1.text) 
end if 
end sub 
本文來源于網頁設計愛好者web開發社區http://www.html.org.cn收集整理,歡迎訪問。