检查如果某些文本框为空 [英] check if certain textboxes are empty

查看:102
本文介绍了检查如果某些文本框为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我需要检查,如果只有4的15文本​​框是空的,但没有运气。我想:

 如果txtbox1.text =然后
lblerror1.visible =真
exitsub
其他
喇嘛喇嘛
万一

但是左的错误文本,并没有看到用户进入文本框中的文本,所以我看了一下,发现string.isnullorwhitespace(string.value)...
嗯,没有告诉我如何使用它,所以我更多的搜索,发现这样的:
如果string.isnullorwhitespace(textbox.text),那么..

这是它这里是很好的结果。现在如果我只能得到一个FOR-NEXT或做助益,而只chaeck 4文本个行业我需要检查,而不是所有文本框。

ASPX页面code:

 < ASP:标签ID =lblerror=服务器文本=页面错误可见=假前景色=红/>< BR / >
< ASP:文本框ID =改为txtName=服务器WIDTH =100像素/>< ASP:标签ID =nameblankerrorRUNAT =服务器文本=不能为空前景色=红可见=假/>< BR />
< ASP:文本框ID =txtcompname=服务器WIDTH =100像素/>< ASP:标签ID =compblankerrorRUNAT =服务器文本=不能为空前景色=红可见=假/>< BR />
< ASP:按钮的ID =btnSubmit按钮=服务器文本=提交/>< BR />
< ASP:标签ID =LBL1=服务器可见=真文本=文本框1/>< ASP:标签ID =lblname=服务器/>< BR />
< ASP:标签ID =LBL2=服务器可见=真文本=文本框2:/>< ASP:标签ID =lblCompName=服务器/>

和为后端code:

 保护小组btnSubmit_Click(发送者为对象,E作为EventArgs的)把手btnSubmit.Click
    测试,看看某些字段为空
    昏暗STR1的String = txtName.Text
    昏暗的STR2赛车作为字符串= txtcompname.Text
    昏暗CatchMe为整数= 0    如果String.IsNullOrWhiteSpace(txtName.Text)然后
        nameblankerror.Visible = TRUE
        CatchMe + = 1
    其他
        nameblankerror.Visible = FALSE
        lblname.text = str1.Trim()
        CatchMe + = 0
    万一
    如果String.IsNullOrWhiteSpace(txtcompname.Text)然后
        compblankerror.Visible = TRUE
        CatchMe + = 1
    其他
        compblankerror.Visible = FALSE
        lblCompName.Text = str2.Trim()
        CatchMe + = 0
    万一
    如果CatchMe = 0则
        做类似处理SQL命令
        lblerror.Visible = FALSE
        退出小组
    elseif的CatchMe<> 0,则
        返回页面和流程什么
        lblerror.Visible = TRUE
        退出小组
    万一
结束小组

原来如此。一个简单,易于遵循检查某些文本框出了一堆。
就像我上面,如果我能想出​​如何检查只有某些文本框,而不是所有文本框,即,使code较短的将是巨大的说明。我把一个catchme这样,如果一个盒子里弥漫于它不会显示出他们需要填写一个也(在错误)的用户,但会赶上其他的空文本框。
要清楚,如果我有15个文本框,但只关心他们4个是不是空的,这是我的检查做。至于是不是需要它,我没有为每一个文本框,做到这一点。


解决方案

  1. 添加独特的的CssClass 所有4 TESTBOX

  2. 找到持有父控件的所有4 文本框

  3. 然后再试试下面的code。

如果您已经申请 validateempty 的CssClass 文本框如果 CTRL1 是保存文本框,然后父控件。

 对于每Ctrl键控制在Ctrl1.Controls
 昏暗的txt文本框为= TryCast(CTRL,文本框)
 如果txt的状态并没有进行任何操作,txt.CssClass =validateempty和txt.Text.Trim()=那
  您的选择code听到的。
 万一下一个

使用jQuery你可以找到它的的CssClass 名称的元素。
首先,JQuery的引用添加到您的网页。你可以找到它听见
其次添加的功能,以的OnClientClick 提交按钮的属性

 函数的validate(){
//既然你已经添加的CssClass的文本框为validateempty,那么
 VAR RET = TRUE;
 $ .find(validateempty)。每个(函数(){
 如果(RET){
  如果($(本).VAL()。修剪()==){
   警报(输入值。);
   $(本)。重点();
   RET = FALSE;
  }
 }
 });
返回RET;
}

$。找到() 会找到与所提供的滤波器参数的所有元素。在这种情况下,CSS类。如果有,因为有4文本框在结果返回多个值,然后循环检查发现个别元素可以在$(本)元素被发现。如果您直接指定内部回归 $。找到(),那么它将从循环无法返回函数

ok, I needed to check if only 4 out of 15 textboxes were empty, but with no luck. I tried:

if txtbox1.text = "" then
lblerror1.visible=true
exitsub
else
bla bla
end if

But that left error text and didn't see the user entering the text in the textbox, so I looked and found string.isnullorwhitespace(string.value)... well, that didn't tell me how to use it so I searched more and found this: if string.isnullorwhitespace(textbox.text) then..

well that was it and here is the outcome. now if I could only get a for-next or do -while to only chaeck the 4 text fileds I need to check and not all textboxes.

ASPX page code:

<asp:Label ID="lblerror" runat="server" Text="Page error" Visible="false" forecolor="red"/><br />
<asp:TextBox ID="txtName" runat="server" Width="100px" /><asp:Label ID="nameblankerror" runat='server' Text="cannot be blank" ForeColor="Red" Visible="false" /><br />
<asp:TextBox ID="txtcompname" runat="server" Width="100px" /><asp:Label ID="compblankerror" runat='server' Text="cannot be blank" ForeColor="Red" Visible="false" /><br />
<asp:Button ID="btnSubmit" runat="server" Text="submit" /><br />
<asp:Label ID="lbl1" runat="server" Visible="true" Text="TextBox 1: " /><asp:label ID="lblname" runat="server" /><br />
<asp:Label ID="lbl2" runat="server" Visible="true" Text="TextBox 2: " /><asp:label ID="lblCompName" runat="server" />

and for the backend code:

    Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    'test to see if certain the fields are blank
    Dim str1 As String = txtName.Text
    Dim str2 As String = txtcompname.Text
    Dim CatchMe As Integer = 0

    If String.IsNullOrWhiteSpace(txtName.Text) Then
        nameblankerror.Visible = True
        CatchMe += 1
    Else
        nameblankerror.Visible = False
        lblname.text = str1.Trim()
        CatchMe += 0
    End If
    If String.IsNullOrWhiteSpace(txtcompname.Text) Then
        compblankerror.Visible = True
        CatchMe += 1
    Else
        compblankerror.Visible = False
        lblCompName.Text = str2.Trim()
        CatchMe += 0
    End If
    If CatchMe = 0 Then
        'do something like process SQL Command
        lblerror.Visible = False
        Exit Sub
    ElseIf CatchMe <> 0 Then
        'return to page and process nothing
        lblerror.Visible = True
        Exit Sub
    End If
End Sub

so that is it. a simple, easy to follow check for certain textboxes out of a bunch. Like I stated above if I could figure out how to check only certain textboxes and not all textboxes, that and make the code shorter would be great. I put in a catchme so that if one box was filled in it wouldn't show the user that they need to fill that one also (in Error), but would catch the other empty textboxes. to make it clear, if I have 15 textboxes, but only care that 4 of them are not empty, this is what I do for the check. I don't do this for every textbox as it is not needed

解决方案

  1. Add unique CssClass to all 4 TestBox.
  2. Find the parent control which holds all 4 TextBox.
  3. And then try the following code.

If you have applied validateempty as CssClass for TextBox and if Ctrl1 is the parent control which holds the textboxes then.

For Each ctrl as Control In Ctrl1.Controls
 Dim txt as TextBox = TryCast(ctrl, TextBox)
 If txt IsNot Nothing And txt.CssClass = "validateempty" And txt.Text.Trim() = "" Then 
  'Your choice of code hear.
 End If

Next

Using JQuery you can find an element by its cssclass name. First add the JQuery reference to your page You can find it hear. Second add the following function to OnClientClick attribute of the submit button.

function validate(){
//Since you have added cssclass for the textboxes as "validateempty" then
 var ret = true;
 $.find(".validateempty").each(function(){
 if(ret){
  if($(this).val().trim() == ""){
   alert("Enter the value.");
   $(this).focus();
   ret = false;
  }
 }
 });
return ret;
}

$.find() will find all elements with the provided filter parameter. In this case css class. If there are more than one value is returned as there are 4 text boxes, then loop through the result and check individual found element which can be found in $(this) element. If you specify return directly inside the $.find(), then it will return from the loop not from function.

这篇关于检查如果某些文本框为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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