简体   繁体   English

在aspx页面上的Visual Studio中使用JavaScript进行验证

[英]validation using javascript in visual studio on an aspx page

The email id is entered into a textbox and the validation for email is applied but there seems to be an error that the whole function is probably not called during execution 电子邮件ID被输入到文本框中并应用了电子邮件验证,但是似乎存在一个错误,即在执行过程中可能未调用整个函数

  <head runat="server">
        <title></title>
         <script type="text/javascript">
             function IsValidUrl()
             {
                 var emailbox = document.getElementById('<%=TextBox4.Text %>');<!--textbox4 is used to receive the email entered by the user-->
                 var email = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
                 if (textbox.value.length > 0)<!--the email field should be non empty-->
                 {
                     if (email.test(emailbox.value))
                     {
                         return true;
                     }
                     else
                     {

                         alert("Please enter valid Email");<!--incase of an invalid email-->
                         return false;
                     }

                 }
                 else
                 {
                     alert("please enter text");
                     return false;
                 }


             }
            </script>
    </head>
    <body>
        <form id="form1" runat="server">
    <asp:Button ID="Button1" runat="server" style="margin-left: 340px" Text="Submit" Width="96px" OnClick="Button1_Click1" OnClientClick="javascript:IsValidUrl();"/>
      </form>
    </body>``

Probably your function is working correctly but the page gets submitted even after the validation so replace below: 您的功能可能正常运行,但即使在验证之后也要提交页面,因此请替换以下内容:

OnClientClick="javascript:IsValidUrl();"

with

OnClientClick="return javascript:IsValidUrl();"

Edit 编辑

Just figured an error in your JS code if (textbox.value.length > 0) line should be if (emailbox.value.length > 0) so try with replace it. 只需在您的JS代码想出一个错误if (textbox.value.length > 0)行应该是if (emailbox.value.length > 0)所以,取而代之的是尝试。

Another issue I found in document.getElementById('<%=TextBox4.Text %>') which should be document.getElementById('<%=TextBox4.ID %>') and in case of using master pages it should be document.getElementById('<%=TextBox4.ClientID %>') so try replacing it too. 我在document.getElementById('<%=TextBox4.Text %>')发现的另一个问题应该是document.getElementById('<%=TextBox4.ID %>') ,在使用母版的情况下应该是document.getElementById('<%=TextBox4.ClientID %>')因此也尝试替换它。

Hope this will help !! 希望这会有所帮助!

There seems couple of issue with the javascript's IsValidURL function so please replace it with below code. javascript的IsValidURL函数似乎有几个问题,因此请用以下代码替换。

function IsValidUrl()
         {
             var emailbox = document.getElementById('<%=TextBox4.ID %>');
             var email = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
             if (emailbox.value.length > 0)
             {
                 if (email.test(emailbox.value))
                 {
                     return true;
                 }
                 else
                 {

                     alert("Please enter valid Email");
                     return false;
                 }

             }
             else
             {
                 alert("please enter text");
                 return false;
             }


         }

There are mainly two problems 主要有两个问题

  1. <%=TextBox4.Text %> here you want id of the textBox but you are fetching value of the textBox. <%= TextBox4.Text%>在这里,您需要textBox的ID,但是正在获取textBox的值。
  2. textbox.value.length you want to check value of the TextBox4 but its variable name is emailbox, not textbox. 要检查TextBox4的值的textbox.value.length,但其变量名是emailbox,而不是textbox。 So I have just corrected those. 所以我刚刚纠正了那些。

Hope this helps you. 希望这对您有所帮助。

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

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