简体   繁体   English

当Javascript验证返回false时,如何防止服务器端操作?

[英]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.

相关问题 如何禁用javascript确认框服务器端验证错误按钮提交事件? - How to Disable javascript confirm box Server side Validation False button Submit Event? 在php服务器端recaptcha上成功,但通过javascript / ajax返回false? - Succes on php server side recaptcha but returns false via javascript/ajax? 当客户端 onblur 事件被触发并返回 false 时,如何取消文本框的服务器端 OnTextChanged 事件? - How to cancel server-side OnTextChanged event of textbox when client-side onblur event gets fired and returns false? 如何使javascript库在节点js中工作以进行服务器端操作 - how to make a javascript library working in node js for server side operations 如何在laravel 5中验证返回false时获取选项值的名称 - How to get name of an option value when validation returns false in laravel 5 如何使服务器端标签可见从JavaScript错误? - How to Make server side lable visible false from javascript? javascript表单验证即使条件匹配也总是返回true,并且应该返回false - javascript form validation always returns true even when condition matches and should returns false 使用JavaScript发送电子邮件时进行服务器端验证? - Server side validation when using JavaScript to send an email? 强制使用JavaScript时是否需要服务器端验证? - Need of server side validation when javascript is made mandatory? 调用服务器端函数时,javascript返回NaN - javascript returns NaN when calling server side function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM