0

我遇到了 JavaScript 验证问题。

我有以下源代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/Full_Width.master" AutoEventWireup="true" CodeFile="validate.aspx.cs" Inherits="validate" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script type="text/javascript">

function emptelephoneno() {
    var elem = document.getElementById('<%=txtfullname.ClientID%>');
    if (elem.value.length == 0) {
        elem.style.border = '1px solid red';
        document.getElementById('nameError').innerHTML = "Please Enter Your First Name";
        return false;
    }
    else {
        elem.style.border = '';
        document.getElementById('nameError').innerHTML = "";
    }
    return true;
}

function emptelephoneno1() {
    var elem1 = document.getElementById('<%=TextBox1.ClientID%>');
    if (elem1.value.length == 0) {
        elem1.style.border = '1px solid red';
        document.getElementById('Span1').innerHTML = "Please Enter Your First Name";
        return false;
    }
    else {
        elem1.style.border = '';
        document.getElementById('Span1').innerHTML = "";
    }

    return true;
}

</script>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div class="wrapper row3">
    <div id="container" class="clear">
        <div id="content" style="border:1px solid green; margin-left:10px; width:600px;">
            <div class="divRegistration">
                <div class="divleft">
                    <label> full name :<span class="spanstar">*</span></label>
                </div>
            <div class="divright">
                <asp:TextBox ID="txtfullname" runat="server" MaxLength="30"></asp:TextBox>
                <span id="nameError" class="spanerror"></span>
            </div>
        </div>

        <div class="divRegistration">
            <div class="divleft">
            <label> full name :<span class="spanstar">*</span></label>
        </div>
        <div class="divright">
            <asp:TextBox ID="TextBox1" runat="server" MaxLength="30"></asp:TextBox>
            <span id="Span1" class="spanerror"></span>
        </div>
    </div>

    <asp:Button ID="btn" runat="server" text="submit" OnClientClick="return emptelephoneno();return emptelephoneno1();" 
        onclick="btn_Click" />
</div>
</div>
</div>
</asp:Content>

在 aspx.cs 页面中,我编写了以下代码:

this.txtfullname.Attributes.Add("onblur", "javascript:emptelephoneno();");
this.TextBox1.Attributes.Add("onblur", "javascript:emptelephoneno1();");

当我单击按钮时,第一个验证实际上有效,但第二个无效。

你能提供任何建议吗?

4

2 回答 2

0

您永远不能在 return 语句之后添加任何语句,因为这些语句将无法访问。

尝试改变:

OnClientClick="return emptelephoneno();return emptelephoneno1();" 

类似于:

OnClientClick="return validate();" 

& 在 JS 中:

function validate() { return emptelephoneno() & emptelephoneno1(); } 
于 2012-11-20T06:10:36.620 回答
0

尝试这个

function validateform() {
  if (emptelephoneno() == true) {
     if (emptelephoneno1() == true) {
       return true;
     }
  }
  return false;
}

<asp:Button ID="btn" runat="server" 
            Text="submit" 
            OnClientClick="return validateform();" />
于 2012-11-20T06:11:39.200 回答