使用Javascript进行C#文本框验证 [英] C# Textbox Validation with Javascript

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

问题描述

大家好,

我陷入了项目的一部分,即验证.我有一个带有文本框和下拉列表的设备输入字段.我编写了一个javascript,直到在ONE文本框中输入了某些内容后,才禁用提交按钮,但是如果其他字段为空,它只会使按钮保持打开状态.

我想让客户端在所有字段中输入,然后启用提交"按钮.

这是我写的Javascript


Hey guys,

I''m stuck on one part of my project, the validation. I have an Equipment entry field with text boxes and drop down lists. I''ve written a javascript that Disables the submit button until something is entered into ONE textbox, but then it just leaves the button open if the other fields are empty.

I want to have the client enter in all the fields then the submit button become enabled.

Here is the Javascript I have written


function Validate() {

      if (document.getElementById("<%=btnSubmit.ClientID%>").disabled == true) {
              var serial = document.getElementById("<%=btnSubmit.ClientID%>");
              serial.disabled = false;
          }

      }



我有5个文本框,我想确保在启用提交之前它们都包含文本.

我只有一个盒子有一条生产线,但是我不知道如何为许多盒子做这条生产线?



I''ve 5 text boxes and I want to ensure they all have text in them before the submit becomes enabled.

I have a line for just one box, but I don''t know how to do it for many boxes?

SerialNum.Attributes.Add("OnKeyDown", "Validate()");



有人可以为此提供帮助吗?页面加载中的Switch语句可能吗?如果是的话,我该怎么写才是最令人困惑的地方.如果您什至可以做2个文本框,我自己去做其余的事情.

谢谢你们,我希望这是有道理的,您可以提供帮助.



Would anyone be able to provide help for this? A Switch statement in the page load maybe? and if so HOW do I write it is the most confusing bit. If you could do even 2 text boxes I''d do the rest myself.

Thanks Guys and I hope this made sense and you can help out.

推荐答案

此处的逻辑是遍历所有输入控件,并查看它们是否存在值.如果其中任何一个仍然为空,请不要启用提交"按钮.

可能有几种方法可以做到这一点.如果您只有用于获取数据的输入"控件,则只需遍历所有这些控件即可.否则就由控制去控制.
例如:
使用普通的Javascript:
The logic here would be to loop through all the input controls and see if values exists for them. If any one of them is still empty, don''t enable the submit button.

There can be few ways to do it. If you have just the ''input'' control for taking data then just loop through all of them. Or else go control by control.
example:
Using normal Javascript:
function Validate() 
{
   var inputField1 = document.getElementById("<%=inputField1.ClientID%>");
   var inputField2 = document.getElementById("<%=inputField2.ClientID%>");
   if(inputField1.value.length > 0 && inputField2.value.length >  0)
      document.getElementById("<%=btnSubmit.ClientID%>").disabled = false;
   else
      document.getElementById("<%=btnSubmit.ClientID%>").disabled = true;
}



CodeBehind文件:



CodeBehind file:

inputField1.Attributes.Add("onblur", "Validate()");
inputField2.Attributes.Add("onblur", "Validate()");



这应该做.


P.S .:有一种方法可以使使用jQuery变得容易,但这需要一些知识.
PS2:也有验证程序控件,但是当前解决方案基于要求.



This should do.


P.S.: There is way to make it easy using jQuery but that would need a little knowledge about it.
P.S.2: There are validator controls too that does it but current solution is based on as asked.


相反,为什么不使用RequiredFieldValidator为您的所有文本框启用文本框.如前所述,在提交之前需要确保文本框应具有值,最好使用RequiredFieldValidator.例如,请参见以下链接:
必需的字段验证器 [ required-field-validator [必填字段验证器 [
Instead, why don''t you use RequiredFieldValidator for all your textboxes and enable the textbox. As you have mentioned that you need to make sure that textboxes should have values before submitting, it is better to use RequiredFieldValidator. See below links for example:
Required Field Validator[^]
required-field-validator[^]
Required Field Validator[^]


您要让客户端在所有字段中输入,然后启用提交按钮.
我给你一个笼统的主意,你为此做什么.在javascript中,您可以使
之类的功能
you want to have the client enter in all the fields then the submit button become enabled.
i give you globle idea what you do for this. in javascript you make function like

function validation()
{
 if(


这篇关于使用Javascript进行C#文本框验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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