该验证码将做什么? [英] what will this validation code will do?

查看:84
本文介绍了该验证码将做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private bool ValidateNumbers()
        {
            if (textBox2.Text == string.Empty)
            {
                return true;
            }
            char[] testArr = textBox2.Text.ToArray();
            bool stat = false;
            for (int i = 0; i < testArr.Length; i++)
            {
                if (!char.IsNumber(testArr[i]))
                {
                    stat = true;
                }
            }
            return stat;
        }



我正在使用方法和validatenumber开发验证控件.
在网上找到了这段代码.但是我不知道这个逻辑背后的逻辑



i am developing a validation control using a method and validatenumber
found this piece of code on net. but i dont know the logic behind this one

推荐答案

此代码用于验证文本框,意味着如果文本框为空或文本框不包含数字,则它将返回true.
this code is for validating textbox mean that if textbox is empty or textbox does not contain number then it will return true.


首先将文本框值存储在字符数组中.
如果不是数字,则逐个字符地检查值,然后将其设置为stat = true.
ex -u在您的texbox中输入"123s",然后由于最后一个字符而将返回stat = true.
使用断点,那么您可以轻松理解.
firstly it storing your textbox value in character array .
then its checking value character by character if it is not a number then it will set stat=true.
ex -u enter ''123s'' in your texbox then it will return stat=true due to last character .
use break point then u can unerstand easily .




我对您所需的验证的猜测是:
如果文本为空白或数字字符串,则返回true. (即,如果输入了文本,则应为数字.允许为空格.)

因此在上述功能中,一旦需要进行更改:
Hi,

What I guess about your needed validation is :
return true if the text is either blank or numeric string. (i.e. if the text is entered then it should be numeric. Blanks are allowed.)

so in above function once change will be required :
char[] testArr = textBox2.Text.ToArray(); 
bool stat = false; 

for (int i = 0; i < testArr.Length; i++) 
{ 
 if (!char.IsNumber(testArr[i])) 
   {
    return false;  // when any non digit character is found then no need to check 
                   //any more characters in the string.
   }
 } 
}



返回true;



return true;


这篇关于该验证码将做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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