如何处理VBScript中的验证错误?

发布于 2025-01-31 17:10:17 字数 518 浏览 5 评论 0原文

如何处理vbscript中字符串的验证错误? 我不明白为什么如果我输入字母,我的代码不会返回循环

Dim mark
Dim ask
mark = Chr(10) & "   " & Chr(149) & " "

Do

    ask = InputBox("Select:" & Chr(10) & mark & "1) First" & mark & "2) Second" & mark & "3) All" & Chr(10), "Select a option")
    If IsEmpty(ask) Then WScript.Quit  'Read if canceled
    If IsNumeric(ask) and CStr(CLng(ask)) = ask and ask => 1 and ask <= 3 Then Exit Do
    MsgBox "Choice a valid value", 48, "Ops..."
Loop

How to handle validation errors to string in vbscript?
I don't understand why if I type a letter my code doesn't return to the loop

Dim mark
Dim ask
mark = Chr(10) & "   " & Chr(149) & " "

Do

    ask = InputBox("Select:" & Chr(10) & mark & "1) First" & mark & "2) Second" & mark & "3) All" & Chr(10), "Select a option")
    If IsEmpty(ask) Then WScript.Quit  'Read if canceled
    If IsNumeric(ask) and CStr(CLng(ask)) = ask and ask => 1 and ask <= 3 Then Exit Do
    MsgBox "Choice a valid value", 48, "Ops..."
Loop

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风不识月 2025-02-07 17:10:17

如果是字符,则您的如果语句将生成错误,因为您正在转换ask(在这种情况下是字符串)到长(clng(clng)(问))。您应该检查ask是数字,然后在另一个语句中检查值:

If IsNumeric(ask) Then
    If CStr(CLng(ask)) = ask And ask >= 1 And ask <= 3 Then Exit Do
End If

In case of a character, your If statement will generate an error because you are converting ask (which is a String in this scenario) to a Long (CLng(ask)). You should check if ask is numeric and then, in another statement, check the value:

If IsNumeric(ask) Then
    If CStr(CLng(ask)) = ask And ask >= 1 And ask <= 3 Then Exit Do
End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文