验证输入框中的用户输入 [英] Validating user input in inputbox

查看:104
本文介绍了验证输入框中的用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行以下代码段并输入可接受的值时,将获得所需的结果.

When I run the following snippet, and enter an acceptable value, I get the desired result.

do while len(strselect) = 0  'or strselect<>"1" or strselect<>"2" or strselect<>"3"
strselect = inputbox ("Please select:" &vbcrlf&vbcrlf&_  
"1. Add an entry" &vbcrlf&vbcrlf&_  
"2. Remove an entry" &vbcrlf&vbcrlf&_  
"3. Search for an entry" &vbcrlf, "Contact Book")
if isempty(strselect) then
wscript.quit()
elseif strselect="1" then
wscript.echo "You chose 1"
elseif strselect="2" then
wscript.echo "You chose 2"
elseif strselect="3" then
wscript.echo "You chose 3"
end if
loop

但是,如果我尝试进一步限制验证过程(通过在do while条件中包括备注)并再次运行代码段,则会触发相应的if条件,但do循环会继续进行,退出.

However if I try constrain the validation process further (by including the remark in the do while conditions), and run the snippet again, I get the corresponding if condition triggered, but the do loop continues, instead of exiting.

我尝试在do循环strselect条件下​​使用isnumericcstr,但没有任何乐趣……我想让该死的东西退出循环会丢失什么?

I've tried using isnumeric and cstr on the do loop strselect conditions, with no joy... What am I missing to get the darn thing to exit the loop?

推荐答案

条件中的逻辑有问题

         condition 1            condition 2       condition 3       condition 4
         v----------------v     v------------v    v------------v    v............v
do while len(strselect) = 0  or strselect<>"1" or strselect<>"2" or strselect<>"3"

根据strselect中的值,您拥有

Depending on value inside strselect, you have

value   c1      c2      c3      c4    
        len=0   <>"1"   <>"2"   <>"3"    c1 or c2 or c3 or c4
--------------------------------------   --------------------
empty   true    true    true    true            true
  1     false   false   true    true            true
  2     false   true    false   true            true
  3     false   true    true    false           true
other   false   true    true    true            true

在每一行中,至少有一个条件被评估为true,并且当您使用Or运算符将条件连接在一起时(如果至少一个值为true,则评估为true),将评估完整条件正确,代码不断循环

In each line you have at least one condition evaluated as true, and as you are concatenating the conditions with Or operators (evaluate to true if at least one of the values is true), the full condition is evaluated as true and the code keeps looping

您只需要更改条件

Do While strselect<>"1" And strselect<>"2" And strselect<>"3"
Do While Not (strselect="1" Or strselect="2" Or strselect="3")
....

这篇关于验证输入框中的用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆