简体   繁体   English

使用JavaScript在ASP.NET中进行验证

[英]validation in asp.net using javascript

 <script type="text/javascript">
    var msg;
    function req1(form)
    {

    if (form.textbox1.value == "" || form.textbox2.value == "")
    {   
       msg= "Please Enter Username And Password";
       return false; 
    }
    else
    {
         return true;
    }

}

    </script>

This is my javascript and i want to use it in asp.net form..so hw can i saw error msg in defalt.aspx page...or hw can i use return value(like true or false)in asp.net 这是我的Javascript,我想以asp.net形式使用它。所以,我可以在defalt.aspx页中看到错误消息吗?还是可以在asp.net中使用返回值(如true或false)

You could use a requiredfieldvalidator for both textbox1 and textbox2 or if you need to validate them at the same time you can use a customvalidator and hookup your clientside javascript like done here: http://www.geekzilla.co.uk/ViewD27B15B4-71A4-4258-81EE-9445FAA9F634.htm 您可以对textbox1和textbox2都使用requiredfieldvalidator,或者如果需要同时验证它们,则可以使用customvalidator并连接客户端JavaScript,如此处所做的操作: http : //www.geekzilla.co.uk/ViewD27B15B4-71A4 -4258-81EE-9445FAA9F634.htm

if you need to hard reference your serverside textboxes make sure you use <%= textbox1.ClientID %> in your javascript. 如果您需要硬引用服务器端文本框,请确保在JavaScript中使用<%= textbox1.ClientID%>。

ASP.NET provides the CustomValidator control with which you can validate multiple controls on the client and serverside with custom validation logic. ASP.NET提供了CustomValidator控件,您可以使用该控件使用自定义验证逻辑在客户端和服务器端验证多个控件。

Take a look at http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.clientvalidationfunction.aspx for a small example. 看看http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.customvalidator.clientvalidationfunction.aspx作为一个小示例。

thnkx i gt solution frm another user... thnkx我gt解决方案frm另一个用户...

function vali() { if (document.getElementById("").value == "") { var m = document.getElementById(""); 函数vali(){if(document.getElementById(“”)。value ==“”){var m = document.getElementById(“”); m.innerHTML= "Please Enter the Name First"; m.innerHTML =“请先输入名称”; return false; 返回false; } else m = document.getElementById(""); } else m = document.getElementById(“”); m.innerHTML=""; m.innerHTML =“”; return true; 返回true; } }

 <asp:Label ID="L1" runat="server" Text="Name"/> <br /> <br /> <asp:TextBox ID="T1" runat="server" /> <br/> <br/> <asp:Button ID="B1" runat="server" OnClientClick="return vali()" /> <br/> </div> </form> 

in the submit button add this attribute 在提交按钮中添加此属性

onclientclick="req1(document.form[0])"

This will call the clientside javascript before submitting to the server. 在提交到服务器之前,这将调用客户端javascript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM