大家快来看啊,

netwar 2003-04-14 12:20:10

public function check(phone)
phonenumber="0123456789"
for i=1 to len(phone)
for j=0 to len(phonenumber)
if mid(phone,i,1)=mid(phonenumber,j,1)
check=true
else
check=false
end if
next
next
end function
我这个程序是想检测用户输入的电话号码,如果不是数字的话,就会认为是错误,请问为什么调试时会出错,要帮忙,谢谢
...全文
96 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
spland 2003-04-14
  • 打赏
  • 举报
回复
做法太奇怪了

兩種做法
1.寫代碼控制只能輸入數字
If (KeyAscii < vbKey0 Or KeyAscii > vbKey9) And KeyAscii <> vbKeyBack And KeyAscii <> vbKeyReturn Then
KeyAscii = C_ZERO
End If
2.check
把你的方法優化一下
用InStr方法
有一個更好的函數,很久沒用想不起來了,試著幫你找一下
rainstormmaster 2003-04-14
  • 打赏
  • 举报
回复
if mid(phone,i,1)=mid(phonenumber,j,1)这句后面少了一个then
public function check(phone)
phonenumber="0123456789"
for i=1 to len(phone)
for j=0 to len(phonenumber)
if mid(phone,i,1)=mid(phonenumber,j,1) Then
check=true
else
check=false
end if
next
next
end function
IMHELLFIRE 2003-04-14
  • 打赏
  • 举报
回复
这个算法有问题,改为
Public Function check(phone)
Dim phonenumber As String
Dim i As Integer, j As Integer
phonenumber = "0123456789"
For i = 1 To Len(phone)
For j = 1 To Len(phonenumber)
If Mid(phone, i, 1) = Mid(phonenumber, j, 1) Then
check = True
Exit For
Else
check = False
End If
Next
Next
End Function
试一试。
spland 2003-04-14
  • 打赏
  • 举报
回复
看了一下mid的第一個參數從1開始
你從0開始,所以出錯
netwar 2003-04-14
  • 打赏
  • 举报
回复
谢谢大家,好象大学都偏了一点吧,
如果象上面几个的算法,好象只判断第一个字符就退出了,这不符合条件,呵,
rainstormmaster 2003-04-14
  • 打赏
  • 举报
回复
另外,如果用户输入带区号的电话号码呢,如 010-12345678,怎么处理,这点恐怕你遗漏了吧。
spland 2003-04-14
  • 打赏
  • 举报
回复
找到了strspn,不過突然發現是c的

不好意思!
rainstormmaster 2003-04-14
  • 打赏
  • 举报
回复
算法也有问题,在判断出含有非数字字符之后,就应该停止判断了(用Exit For或者Exit Function)。
参考一下这个:
' writen by wangluonanhai(网络男孩)
' 禁止在textbox中输入指定格式之外的字符
Option Explicit
Dim savetext As String
Dim saveselstart As Long
Private Sub Text6_Change()
Static nestedcall As Boolean
If nestedcall Then Exit Sub
If IsNumeric(Text6.Text) Then
savetext = Text6.Text
saveselstart = Text6.SelStart
Else
nestedcall = True
Text6.Text = savetext
nestedcall = False
Text6.SelStart = saveselstart
End If
End Sub

Private Sub Text6_GotFocus()
savetext = Text6.Text
saveselstart = Text6.SelStart
End Sub

Private Sub Text6_KeyUp(KeyCode As Integer, Shift As Integer)
saveselstart = Text6.SelStart
End Sub

Private Sub Text6_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
saveselstart = Text6.SelStart
End Sub

Private Sub Text6_MouseMove(Button As Integer, Shift As Integer, x As Single, Y As Single)
saveselstart = Text6.SelStart
End Sub

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧