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

时间:2016-01-31 13:04:02

标签: javascript asp.net visual-studio

将电子邮件ID输入文本框并应用电子邮件验证,但似乎有一个错误,即在执行期间可能未调用整个函数

[Sun Jan 31 16:59:14.077148 2016] [core:alert] [pid 6492:tid 916] [client 127.0.0.1:63045] D:/lamp/www/forum/.htaccess: Invalid command 'AddOutputFilterByType', perhaps misspelled or defined by a module not included in the server configuration, referer: http://forum.dev/core/install/install.php
[Sun Jan 31 17:17:38.028320 2016] [core:alert] [pid 6492:tid 916] [client 127.0.0.1:63417] D:/lamp/www/forum/.htaccess: Invalid command 'AddOutputFilterByType', perhaps misspelled or defined by a module not included in the server configuration
[Sun Jan 31 17:17:38.191406 2016] [core:alert] [pid 6492:tid 916] [client 127.0.0.1:63418] D:/lamp/www/forum/.htaccess: Invalid command 'AddOutputFilterByType', perhaps misspelled or defined by a module not included in the server configuration, referer: http://forum.dev/core/install/install.php

2 个答案:

答案 0 :(得分:0)

可能您的功能正常工作,但即使在验证后页面也会提交,因此请在下面替换:

OnClientClick="javascript:IsValidUrl();"

OnClientClick="return javascript:IsValidUrl();"

修改

刚刚认为您的JS代码if (textbox.value.length > 0)行中的错误应为if (emailbox.value.length > 0),请尝试替换它。

我在document.getElementById('<%=TextBox4.Text %>')中发现的另一个问题应该是document.getElementById('<%=TextBox4.ID %>'),如果使用母版页,则应该document.getElementById('<%=TextBox4.ClientID %>'),所以请尝试替换它。

希望这会有所帮助!!

答案 1 :(得分:0)

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;
             }


         }

主要有两个问题

  1. &lt;%= TextBox4.Text%&gt;在这里你想要textBox的id,但你要获取textBox的值。
  2. textbox.value.length要检查TextBox4的值,但其变量名称是emailbox,而不是textbox。 所以我刚刚纠正了这些。
  3. 希望这会对你有所帮助。