C# 如何检查asp文本框是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13528117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if a asp text box is empty or not
提问by Mathematics
Possible Duplicate:
textbox empty check using javascript
可能的重复:
使用 javascript 的文本框空检查
I have a asp.net button and a asp.net textbox, when I click on button, I want to check if textbox is empty or not but not sure how I can do that,
我有一个 asp.net 按钮和一个 asp.net 文本框,当我点击按钮时,我想检查文本框是否为空,但不知道如何做到这一点,
<div>
<asp:TextBox ID="txtEU" runat="server"></asp:TextBox>
</div>
<div>
<asp:ImageButton ID="button" runat="server" OnClientClick="MyFunction(); return false;" ImageUrl="/myfolder/abc.png" />
</div>
in my JavaScript I am doing,
在我正在做的 JavaScript 中,
<script type="text/javascript">
function doWork()
{
if($input[]
not sure how to check if its empty or not, if its empty then I am doing something if not then it should call a code behind method for that button.
不知道如何检查它是否为空,如果它是空的,那么我正在做一些事情,如果不是,那么它应该调用该按钮的代码隐藏方法。
采纳答案by codingbiz
Read on the ClientIDMode propertyto see how element ID are generatedin ASP.NET (4.0 and above)
阅读ClientIDMode 属性以了解如何在 ASP.NET(4.0 及更高版本)中生成元素 ID
function doWork()
{
var textbox = document.getElementById('<%=txtEU.ClientID%>');
if(textbox.value.length == 0)
{
}
}
OR
或者
if(textbox.value == "")
Using Validators will help you handle some of this validation out of the box. One of them is RequiredValidator, which evaluates the value of an input control to ensure that the user enters a value.
使用验证器将帮助您处理一些开箱即用的验证。其中之一是RequiredValidator,它评估输入控件的值以确保用户输入一个值。
<asp:RequiredFieldValidator runat="server" ID="txtEURequiredValidator" ErrorMessage="EU should not be empty" />
回答by Lionel D
you have the ability to use a RequiredFieldValidator or a CustomValidator if you need to execute a more complex scenario.
如果您需要执行更复杂的场景,您可以使用 RequiredFieldValidator 或 CustomValidator。
Here is a good starting point i think: http://asp.net-tutorials.com/validation/introduction/(check the links on the right side to have a detailed view of the validators)
我认为这是一个很好的起点:http: //asp.net-tutorials.com/validation/introduction/(检查右侧的链接以详细了解验证器)
Hope this helps.
希望这可以帮助。
回答by StuartLC
You can do like so:
你可以这样做:
if ($('#<%= txtEU.ClientID %>').val()({
// String is not empty
}
Explanation:
解释:
- Because, by default, asp.net mangles the html ID for the text box, you will need to inject the name into your jQuery.
- In jQuery, null and empty can both be tested for with !
- 因为默认情况下,asp.net 会修改文本框的 html ID,所以您需要将名称注入 jQuery。
- 在 jQuery 中,null 和 empty都可以用 !
回答by Atul Phadtare
//javascript code
function Myfunction()
{
if(document .getElementById("<%=txtEU.ClientID %>").value=="")
{
alert("Please Enter Text");
txtEU.focus();
return false;
}
return true;
}
//aspcode
<asp:ImageButton ID="button" runat="server" OnClientClick="return Myfunction();" ImageUrl="/myfolder/abc.png" />
回答by Sharmaji TM
if ($('#<%= yourtextboxname.ClientID %>').val() =="")
// String is not empty
}

