Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要一个javascript函数来标识单击“保存”按钮时使用的文本框_Javascript_C#_Asp.net_Client Side Validation - Fatal编程技术网

需要一个javascript函数来标识单击“保存”按钮时使用的文本框

需要一个javascript函数来标识单击“保存”按钮时使用的文本框,javascript,c#,asp.net,client-side-validation,Javascript,C#,Asp.net,Client Side Validation,我在同一ASP.net页面的3个选项卡中有3个文本框,以及相应的3个具有相同功能的javascript函数。目前我正在为三个文本框使用三种不同的功能。我只需要一个javascript函数,而不是三个。请帮帮我 <asp:TextBox ID="textBox1" runat="server" ValidationonGroup="abcd"></asp:TextBox> <asp:CustomValidator ID="ID1" runat="server" Val

我在同一ASP.net页面的3个选项卡中有3个文本框,以及相应的3个具有相同功能的javascript函数。目前我正在为三个文本框使用三种不同的功能。我只需要一个javascript函数,而不是三个。请帮帮我

<asp:TextBox ID="textBox1" runat="server" ValidationonGroup="abcd"></asp:TextBox>
<asp:CustomValidator ID="ID1" runat="server" ValidationGroup="abcd" ControlToValidate="textBox1" ErrorMessage="message" ClientValidationFunction="fname"></asp:CustomValidator>

Javascript函数

function fname(sender, args) {
 var x = document.getElementById('<%=txtBox1.ClientID%>').value;
 if (some condition) { 
args.IsValid = false; 
} else { 
args.IsValid = true;
}
}
函数fname(发送方,参数){
var x=document.getElementById(“”).value;
如果(某些条件){
args.IsValid=false;
}否则{
args.IsValid=true;
}
}

一种方法是检查哪个文本框值!=“”并基于此执行发送

例如,onClick处理程序可能包含这个

if(document.getElementById('<%=txtBox1.ClientID%>').value != ""){ 
do this
} else if (document.getElementById('<%=txtBox2.ClientID%>').value != ""){ 
do this
} else {
do this
}
if(document.getElementById(“”).value!=“”){
这样做
}else if(document.getElementById(“”).value!=“”){
这样做
}否则{
这样做
}

一种方法是检查哪个文本框值!=“”并基于此执行发送

例如,onClick处理程序可能包含这个

if(document.getElementById('<%=txtBox1.ClientID%>').value != ""){ 
do this
} else if (document.getElementById('<%=txtBox2.ClientID%>').value != ""){ 
do this
} else {
do this
}
if(document.getElementById(“”).value!=“”){
这样做
}else if(document.getElementById(“”).value!=“”){
这样做
}否则{
这样做
}

为什么不创建一个函数来接收文本框ID并从其他3个函数调用它:

function fname(textBoxID, sender, args) {
  var x = document.getElementById(textBoxID).value;
  if (some condition) { 
    args.IsValid = false; 
  } else { 
  args.IsValid = true;
  }
}
当您为第一个文本框调用它时,只需将“”作为第一个参数传递。同样,将“”传递给第二个文本框

还有另一个解决方案,您可以在此处找到更详细的解决方案:


简而言之,它是这样的:使用sender.id获取CustomValidator的id。然后,只要验证器的id是文本框的id,前缀为“validator”,我就可以使用javascript替换来删除“validator”,从而找到文本框id。

为什么不创建一个函数来接收文本框id并从其他3个函数调用它:

function fname(textBoxID, sender, args) {
  var x = document.getElementById(textBoxID).value;
  if (some condition) { 
    args.IsValid = false; 
  } else { 
  args.IsValid = true;
  }
}
当您为第一个文本框调用它时,只需将“”作为第一个参数传递。同样,将“”传递给第二个文本框

还有另一个解决方案,您可以在此处找到更详细的解决方案:


简而言之,它是这样的:使用sender.id获取CustomValidator的id。然后,只要验证器的id是文本框的id,前缀为“validator”,我就可以使用javascript替换来删除“validator”,从而找到文本框id。

否,如果这需要工作,您必须确保只填充1个框,这可以在用户更改选项卡时完成。否,如果这需要工作,你必须确保只有1个框被填写,这可以在用户更改选项卡时完成。很好的建议。请帮助我如何像那样传递textBoxId。在html代码中,我只添加了函数名,如ClientValidationFunction=“fname”。请告诉我如何像第一个文本框调用函数fname1(sender,args)那样传递textBoxId,并从中调用fname(“”,sender,args);对于第二个文本框调用函数fname2,并从中调用fname(“”,sender,args)等…我已经编辑了我的答案。这个问题有很好的解决方案,请参阅编辑答案中的链接。很好的建议。请帮助我如何像那样传递textBoxId。在html代码中,我只添加了函数名,如ClientValidationFunction=“fname”。请告诉我如何像第一个文本框调用函数fname1(sender,args)那样传递textBoxId,并从中调用fname(“”,sender,args);对于第二个文本框调用函数fname2,并从中调用fname(“”,sender,args)等…我已经编辑了我的答案。这个问题有很好的解决方案,请参见编辑答案中的链接。