[英]Validation for my ASP textboxes is not working using javascript and ASP.NET
[英]Email Address validation with Javascript not working in ASP.net
我正在使用以下代碼來驗證電子郵件地址,並將電子郵件地址與重新輸入電子郵件地址進行比較。 但是,當我輸入錯誤的電子郵件地址格式並單擊“注冊”時,不知何故我沒有彈出窗口。
<%@ Page Title="" Language="VB" MasterPageFile="~/Main.master" AutoEventWireup="false"
CodeFile="Registration.aspx.vb" Inherits="Registration" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<script type="text/javascript" language="javascript">
function verifyEmail() {
var status = false;
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
if (document.aspnetForm.txtEmailAddress.value.search(emailRegEx) == -1)
{
alert("Please enter a valid email address.");
}
else if (document.aspnetForm.txtEmailAddress.value != document.aspnetForm.txtVerifyEmailAddress.value)
{
alert("Email addresses do not match. Please retype them to make sure they are the same.");
}
else
{
alert("Woohoo! The email address is in the correct format and they are the same.");
status = true;
}
alert(status)
return status;
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<table >
<tr>
<td >
<asp:Label ID="Label3" runat="server" Text="Email Address"></asp:Label>
</td>
<td >
<asp:TextBox ID="txtEmailAddress" Width="234px" runat="server"></asp:TextBox>
</td>
<td >
</td>
</tr>
<tr>
<td >
<asp:Label ID="Label4" runat="server" Text="Re-Enter Email"></asp:Label>
</td>
<td >
<asp:TextBox ID="txtVerifyEmailAddress" Width="234px" runat="server"></asp:TextBox>
</td>
<td >
</td>
</tr>
<tr>
<td >
</td>
<td >
<asp:Button ID="btnRegister" runat="server" Text="Register" OnClientClick ="verifyEmail();" value="Check Email Address"/>
<asp:Button ID="btnCancel" runat="server" Text="Cancel" />
</td>
<td >
</td>
</tr>
</table>
</asp:Content>
嘗試這個
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\
".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA
-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
根據您的需要使用以上功能。
ASP .NET具有正則表達式驗證器和比較驗證器,您應該使用它們
這里有他們的聯系
http://asp-net-example.blogspot.in/2009/02/aspnet-regularexpressionvalidator.html和http://asp.net-tutorials.com/validation/compare-validator/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.