Javascript客户端验证在ASP.Net中不起作用
我在母版页中有一个webform,其中有两个文本框,我正在尝试在将一些数据保存到数据库之前验证这些文本框 如果我在一个简单的应用程序(比如简单的登录)中测试Javascript代码,它就会工作。但当我在我的应用程序中使用它时,我无法工作。它只是将空空间保存到数据库中 我不知道为什么JS代码不起作用,我也不知道还能做什么 这是我的一些页面代码:Javascript客户端验证在ASP.Net中不起作用,javascript,c#,asp.net,Javascript,C#,Asp.net,我在母版页中有一个webform,其中有两个文本框,我正在尝试在将一些数据保存到数据库之前验证这些文本框 如果我在一个简单的应用程序(比如简单的登录)中测试Javascript代码,它就会工作。但当我在我的应用程序中使用它时,我无法工作。它只是将空空间保存到数据库中 我不知道为什么JS代码不起作用,我也不知道还能做什么 这是我的一些页面代码: </head> <body> <form id="form1" class="contact
</head>
<body>
<form id="form1" class="contact-form text-right">
<section class="contact-area" id="contact">
<div class="container-fluid">
<div class="row align-items-center d-flex justify-content-start">
<div class="col-lg-6 col-md-12 contact-left no-padding">
<div>
<%-- tabla aqui --%>
<table style="width: 100%;">
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
</div>
</div>
<div class="col-lg-4 col-md-12 pt-100 pb-100">
<asp:Label runat="server" ID="lblFecha" Text=""></asp:Label>
<asp:Label runat="server" ID="lblIDUsuario" visible="false" Text=""></asp:Label>
<asp:Label runat="server" ID="lblUsuario" Text=""></asp:Label>
<asp:TextBox ID="txtID" visible="true" class="common-input mt-10" type="numeric" readonly="false" runat="server"></asp:TextBox>
<asp:TextBox ID="txtTitulo" placeholder="Ingrese el título" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Ingrese el título'" class="common-input mt-10" type="text" runat="server"></asp:TextBox>
<textarea id="txtContenido" style="resize:none;" cols="20" rows="5" placeholder="Ingrese el contenido" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Ingrese el contenido'" class="common-input mt-10" type="text" runat="server"></textarea>
<asp:Button ID="btnGuardar" OnClientClick="return performCheck();" OnClick="btnGuardar_Click" runat="server" Text="Guardar" class="primary-btn mt-20"></asp:Button>
<asp:Button ID="btnActualizar" OnClientClick="return performCheck();" onClick="btnActualizar_Click" runat="server" Text="Actualizar" class="primary-btn mt-20"></asp:Button>
<asp:Label ID="lblError" runat="server" Text="" ForeColor="Red"></asp:Label>
<div class="alert-msg">
</div>
</div>
</div>
</div>
</section>
</form>
<script type="text/javascript">
function performCheck() {
if (document.getElementById("txtTitulo").value == '') {
alert(" Debe Ingresar un título");
return false;
}
if (document.getElementById("txtContenido").value == '') {
alert("Debe Ingresar un contenido");
return false;
}
return true;
}
</script>
<script src="js/vendor/jquery-2.2.4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
<script src="js/vendor/bootstrap.min.js"></script>
<script src="js/owl.carousel.min.js"></script>
<script src="js/jquery.ajaxchimp.min.js"></script>
<script src="js/jquery.sticky.js"></script>
<script src="js/parallax.min.js"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBhOdIF3Y9382fqJYt5I_sswSrEw5eihAA"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="js/jquery.magnific-popup.min.js"></script>
<script src="js/main.js"></script>
</body>
</html>
函数performCheck(){
if(document.getElementById(“txtitulo”).value=''){
警报(“Debe Ingrear un título”);
返回false;
}
if(document.getElementById(“txtContenido”).value=''){
警报(“Debe Ingrear un contenido”);
返回false;
}
返回true;
}
这是我的JS代码:
<script type="text/javascript">
function performCheck() {
if (document.getElementById("txtTitulo").value == '') {
alert(" Debe Ingresar un título");
return false;
}
if (document.getElementById("txtContenido").value == '') {
alert("Debe Ingresar un contenido");
return false;
}
return true;
}
</script>
函数performCheck(){
if(document.getElementById(“txtitulo”).value=''){
警报(“Debe Ingrear un título”);
返回false;
}
if(document.getElementById(“txtContenido”).value=''){
警报(“Debe Ingrear un contenido”);
返回false;
}
返回true;
}
不要在按钮的点击处理程序中执行表单验证。在表单提交处理程序中执行这些操作。用户可以通过在输入中单击enter来提交表单,而不小心绕过了验证。一个例子可以在
这是一个完美的例子,说明了为什么只有客户端验证是不够的。如果提交的数据是否有效至关重要,则必须实施服务器端验证。无论是偶然还是有意,用户都可以轻松绕过任何客户端验证。首先,使用验证控件会更容易。但是aspnet可以重命名控件的ID,所以您可能需要使用
document.getElementById(“”)。value
就是它!它成功了,谢谢。我更喜欢两者都有:客户端和服务器端。但这是一个家庭作业,老师告诉我们只使用客户端验证。另外,他建议只使用客户端验证,因为服务器端验证需要更多的资源,并且会影响性能。@ErrolChavesMoya,一个多么差劲的老师。你应该两全其美!我认为对性能的影响不是问题。@ErrolChavesMoya,任何人都可以编辑JavaScript代码,因此完全删除您的验证+1关于同时使用服务器端和客户端。