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>``
2 个解决方案
#1
0
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
主要有两个问题
- <%=TextBox4.Text %> here you want id of the textBox but you are fetching value of the textBox.
- textbox.value.length you want to check value of the TextBox4 but its variable name is emailbox, not textbox. So I have just corrected those.
<%= TextBox4.Text%>这里你想要textBox的id,但你要获取textBox的值。
textbox.value.length要检查TextBox4的值,但其变量名称是emailbox,而不是textbox。所以我刚刚纠正了那些。
Hope this helps you.
希望这对你有所帮助。
#2
0
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代码中发现错误,如果(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%>'),如果使用母版页,它应该是文档。 getElementById('<%= TextBox4.ClientID%>')所以请尝试替换它。
Hope this will help !!
希望这会有所帮助!!
#1
0
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
主要有两个问题
- <%=TextBox4.Text %> here you want id of the textBox but you are fetching value of the textBox.
- textbox.value.length you want to check value of the TextBox4 but its variable name is emailbox, not textbox. So I have just corrected those.
<%= TextBox4.Text%>这里你想要textBox的id,但你要获取textBox的值。
textbox.value.length要检查TextBox4的值,但其变量名称是emailbox,而不是textbox。所以我刚刚纠正了那些。
Hope this helps you.
希望这对你有所帮助。
#2
0
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代码中发现错误,如果(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%>'),如果使用母版页,它应该是文档。 getElementById('<%= TextBox4.ClientID%>')所以请尝试替换它。
Hope this will help !!
希望这会有所帮助!!