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收集整理,欢迎访问。