1. Only "0-9" 10 characters in the text box. And the day monthly separator will automatically generate.
2, VB's year is 100-9999, I am limited to 1000-2999, isn't it enough? If you really don't have enough, you can modify your own control. Maybe you will ask: Why don't you use two digits?
my point of view:
1, the two-digit year format is not intuitive.
2. If you encounter information to record the birthday month, it is very difficult to stop.
3, month can only range from 01-12, and when entering 3-9, the system will automatically default 03-09.
4, the system will automatically default to 04-09 when entering 4-9. and also:
A: When the month is 1, 3, 5, 7, 8, 10, 12, the date cannot exceed 31
B: When the month is 4, 6, 9, 11, the date cannot exceed 30
C: When the month is 2, the date cannot exceed 29 days.
D: When the month is 2, it is not a leap year, the date cannot exceed 28
With the above "Advantages", as long as you enter the day and month, you will make sure it is a legal date expression. Friends, hurry to paste the following programs into the VB project (of course, you may change Text1 for other text box numbers such as: Text3) Try it, believe, you will immediately fall in love with her, until forever ... ...
PRIVATE SUB TXT_AGE_CHANGE () DIM A, B, C AS STRING '------------------------------------------------------------------------------------------------------------------------------ -------------------------------------- 'New control' ----- -------------------------------------------------- ------------------- 'Limiting the first place must be "1" or "2", but also the year must be between 1000-2999, enough ! IF LEN (txt_age.text) = 1 Then if Left ((txt_age.text), 1) <> "1" and left, 1) <> "2" THEN TXT_AGE.TEXT = "" END IFEND IF 'limits the second, third and fourth bits must be "1, 2, 3, 4, 5, 6, 7, 8, 9, 0" IF (len (txt_age.text) = 2 or LEN (txt_age.text ) = 3 or LEN (txt_age.text) = 4 THEN IF (Right (txt_age.text), 1) <> "0" and Right ((txt_age.text), 1) <> "1" and _ Right ((txt_age.text), 1) <> "2" and Right ((txt_age.text), 1) <> "3" and _ Right ((txt_age.text), 1) <> "4" and Right ( (txt_age.text), 1) <> "5" and _ right ((txt_age.text), 1) <> "6" and Right ((txt_age.text), 1) <> "7" and _ rights ( (txt_age.text), 1) <> "8" and Right ((txt_age.text), 1) <> "9") THEN TXT_AGE.TEXT = Left (txt_age.text), len (txt_age.text) - 1) TXT_AGE.SELSTART = LEN (txt_age.text) end ifnd ifif len (txt_age.text) = 4 THEN TXT_AGE.TEXT = txt_age.text "/" txt_age.selstart = len (txt_age.text) end if 'at the same year Automatically add "-" separator '--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------- 'Month Enter Control' ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------------------------- if len (txt_age.text) = 6 IF right ((txt_age.text), 1) <> "0"
And Right (TXT_AGE.TEXT), 1) <> "1" THEN IF Right ((txt_age.text), 1) = "2" or Right ((txt_age.text), 1) = "3" or _ Right ((txt_age.text), 1) = "4" or right ((txt_age.text), 1) = "5" or _ Right ((txt_age.text), 1) = "6" or Right ((txt_age) Text), 1) = "7" or _ Right (txt_age.text), 1) = "8" or Right ((txt_age.text), 1) = "9" THEN A = Right ((txt_age.text) , 1) TXT_AGE.TEXT = Left (txt_age.text), 5) "0" A "/" If this, the judgment of the following if, the judgment is naturally jumped automatically .
TXT_AGE.SELSTART = LEN (txt_age.text) Else 'limit can only be entered "0-9" txt_age.text = left (txt_age.text), len (txt_age.text) - 1) txt_age.selstart = LEN (TXT_AGE. TEXT) END if End IFIF LEN (TXT_AGE.TEXT) = 7 Then if Left (Right (TXT_AGE.TEXT, 2), 1) = "0" Then 'If the first bit is "0" IF Right ((txt_age .Text), 1) <> "1" and right ((txt_age.text), 1) <> "2" and _ rights ((txt_age.text), 1) <> "3" and Right ((txt_age). Text), 1) <> "4" and _ right ((txt_age.text), 1) <> "5" and right ((txt_age.text), 1) <> "6" and _ rights ((txt_age). Text), 1) <> "7" and Right ((txt_age.text), 1) <> "8" and _ right ((txt_age.text), 1) <> "9" THEN TXT_AGE.TEXT = Left (txt_age.text), len (txt_age.text) - 1) txt_age.selstart = len (txt_age.text) Else TXT_AGE.TEXT = txt_age.text "/" 'The previous input is automatically added "-" separation TXT_AGE.SELSTART = LEN (txt_age.text) EXIT SUB 'is less! If less, when the month is "01", the next IF ... Endiff is set, which will have a dead cycle here, and an error in the spill stack space! 'Note: This program can be used in several places to use exit sub, you have to add yourself! END IFEND IF
If LEFT (Right (TXT_AGE.TEXT), 2), 1) = "1" "If the first bit is" 1 "if right ((txt_age.text), 1) <>" 0 "and Right ( (txt_age.text), 1) <> "1" and right ((txt_age.text), 1) <> "2" THEN TXT_AGE.TEXT = Left ((txt_age.text), len (txt_age.text) - 1 TXT_AGE.SELSTART = LEN (txt_age.text) Else txt_age.text = txt_age.text "-" Automatically adds a "-" separator TXT_AGE.SELSTART = LEN (txt_age.text) End IFEND IFEND after the previous month IF '------------------------------------------------ -------------------------- "Date Enter the control? '---------------- -------------------------------------------------- --------- if len (txt_age.text) = 9 t if right (txt_age.text), 1) <> "0" and right ((txt_age.text), 1) <> "1 "And Right (TXT_AGE.TEXT), 1) <>" 2 "and right ((txt_age.text), 1) <>" 3 "THEN IF RIGHT ((txt_age.text), 1) =" 4 "OR Right (txt_age.text), 1) = "5" or _ Right ((txt_age.text), 1) = "6" or Right ((txt_age.text), 1) = "7" or _ Right ((( TXT_AGE.TEXT), 1) = "8" or Right ((txt_age.text), 1) = "9" THEN A = Right ((txt_age.text), 1) txt_age.text = Left (txt_age.text), 8) "0" a txt_age.selstart = len (txt_age.text) EXIT SUB 'date is less than 10 The judgment of the following character length is of course correct.
Let it do? Else txt_age.text = left (txt_age.text), len (txt_age.text) - 1) txt_age.selstart = len (txt_age.text) End if End Ifnd if 'When you want to modify the last bit of the date? IF LEN (txt_age.text) = 10 THEN B = Left (Right (TXT_AGE.TEXT, 5), 2) 'Take the month value, used for the following date correctness judge! C = Left (txt_age.text, 4) 'Take the year value, used for the following date correctness! IF Right (TXT_AGE.TEXT), 1) <> "0" and Right ((txt_age.text), 1) <> "1" and _ Right ((txt_age.text), 1) <> "2" and Right (txt_age.text), 1) <> "3" and _ Right (txt_age.text), 1) <> "4" and right ((txt_age.text), 1) <> "5" and _ Right (txt_age.text), 1) <> "6" and Right ((txt_age.text), 1) <> "7" and _ rights ((txt_age.text), 1) <> "8" and Right ((txt_age.text), 1) <> "9" THEN TXT_AGE.TEXT = Left ((txt_age.text), len (txt_age.text) - 1) txt_age.selstart = len (txt_age.text) end if 'limit Enter the illegal character.
If Right (TXT_AGE.TEXT, 2) = "00" THEN TXT_AGE.TEXT = Left ((txt_age.text), len (txt_age.text) - 2) TXT_AGE.SELSTART = LEN (TXT_AGE.TEXT) END IF 'Limit Date Can't be 0 if (b = "01" and Val (Right (TXT_AGE.TEXT, 2))> 31) OR _ (B = "03" and Val (Right (TXT_AGE.TEXT, 2))> 31) OR_ (b = "05" and val (txt_age.text, 2)> 31) OR _ (b = "07" and val (right (txt_age.text, 2))> 31) OR _ (B = " 08 "AND VAL (Right (txt_age.text, 2)> 31) OR _ (b =" 10 "and val (right (txt_age.text, 2))> 31) OR _ (B =" 12 "and Val (Right (txt_age.text, 2))> 31) THEN TXT_AGE.TEXT = Left ((txt_age.text), len (txt_age.text) - 2) TXT_AGE.SELSTART = LEN (txt_age.text) Endiff 'The month The date is not greater than 31 days during the day. IF (b = "04" and val (Right (txt_age.text, 2))> 30) or _ (b = "06" and val (rot (txt_age.text, 2))> 30) OR_ (b = "09" and Val (Right (txt_age.text, 2)> 30) OR _ (B = "11" and Val (Right (txt_age.text, 2))> 30) THEN TXT_AGE.TEXT = Left ((txt_age .Text, len (txt_age.text) - 2) TXT_AGE.SELSTART = LEN (txt_age.text) Endiff 'The date is not greater than 30 when the month is small month. IF b = "02" Then IF VAL (c) MOD 4 <> 0 and val (Right (txt_age.text, 2)> 28 THEN TXT_AGE.TEXT = Left ((txt_age.text), len (txt_age.text) - 2) TXT_AGE.SELSTART = LEN (txt_age.text) Endiff 'Non-leap year Date must not exceed 28.
IF VAL (c) MOD 4 = 0 and Val (Right (txt_age.text, 2))> 29 THEN TXT_AGE.TEXT = Left (txt_age.text), LEN (TXT_AGE.TEXT) - 2) TXT_AGE.SELSTART = LEN (txt_age.text) Endiff 'leap year date must not exceed 29. Endiff 'The date correctness is determined when the date is 2 times! Endiff '----------------------------------------------- ---------------------------- 'No other characters will no longer accept other characters after entering the month. The method is as follows:' ---- -------------------------------------------------- -------------------- 'The first method:' Setting maxlength = 10 'in the attribute window of txt_age:' Text2.Setfocus is Set a jump statement to get the next object to get the focus? 'Third method: if len (txt_age.text) = 11 THENTXT_AGE.TEXT = Left ((txt_age.text), Len (txt_age.text) - 1 ) TXT_AGE.SELSTART = LEN (txt_age.text) end if ------------------------------------- ------------------------------------------