[英]How to prevent server side operations when Javascript Validation returns false?
I'm trying to apply some client side validations using Javascript which is working fine, But even when validation returns false the form goes to server side methods ex. 我正在尝试使用运行良好的Javascript进行一些客户端验证,但是即使验证返回false,该表单也会转到服务器端方法ex。 btnSubmit_Click()
It may be happening because I'm calling the JavaScript function on btnSubmit
like below: btnSubmit_Click()
可能会发生,因为我正在btnSubmit
上调用JavaScript函数,如下所示:
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" OnClientClick="Javascript:validate()"/>
I wants to check whether every Textbox values are valid or not, Then if all are valid then perform server side call ie btnSubmit_Click()
event else remain on same form until all values of TextBoxes are valid. 我想检查每个Textbox值是否有效,然后,如果所有值都有效,则执行服务器端调用,即btnSubmit_Click()
事件,否则保持相同的形式,直到TextBoxes的所有值都有效为止。
How to achieve this on single button? 如何在单个按钮上实现这一目标?
Below is complete JavaScript
function and aspx
code: 以下是完整的JavaScript
函数和aspx
代码:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script language="javascript" type="text/javascript">
function validate() {
var summary = "";
summary += isvalidFirstname();
summary += isvalidEmail();
summary += isvalidphoneno();
if (summary != "") {
alert(summary);
return false;
}
else {
return true;
}
}
function isvalidphoneno() {
var uid;
var temp = document.getElementById("<%=txtPhone.ClientID %>");
uid = temp.value;
var re;
re = /^[0-9]d(10)/;
var digits = /\d(10)/;
if (uid == "") {
return ("Please enter phoneno" + "\n");
}
else if (re.test(uid)) {
return "";
}
else {
return ("Phoneno should be digits only" + "\n");
}
}
function isvalidFirstname() {
var uid;
var temp = document.getElementById("<%=txtName.ClientID %>");
uid = temp.value;
var re = /^[a-zA-Z ]+$/
if (uid == "") {
return ("Please enter firstname" + "\n");
}
else if (re.test(uid)) {
return "";
}
else {
return ("FirstName accepts Characters and spaces only" + "\n");
}
}
function isvalidEmail() {
var uid;
var temp = document.getElementById("<%=txtEmail.ClientID %>");
uid = temp.value;
var re = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
if (uid == "") {
return ("Please Enter Email" + "\n");
}
else if (re.test(uid)) {
return "";
}
else {
return ("Email should be in the form ex:abc@xyz.com" + "\n");
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset>
<legend>Employee Registration Form</legend>
<table>
<tr>
<td>Name: </td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Phone Number: </td>
<td>
<asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Email Id: </td>
<td>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Address: </td>
<td>
<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
onclick="btnSubmit_Click" OnClientClick="Javascript:validate()"/></td>
</tr>
</table>
</fieldset>
</div>
</form>
</body>
</html>
您必须在OnClientClick
返回validate
函数的值:
OnClientClick="return validate();"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.