使用javascript验证? [英] validation using javascript?
问题描述
大家好,
我是javascript的新手。
i用来开发一个asp.net应用程序。
这里我已经做到了使用java脚本使用一个验证过程。
i需要验证我的电子邮件地址。
如果我的电子邮件地址有效,我已重定向到另一个页面。
如果我的电子邮件地址无效,我收到无效的邮件ID。
但我的问题是,
如果我的电子邮件地址有效,我无法重定向到另一个页面。请找到下面的代码
并让我知道错误的地方或者给我一个有效的代码。
我的代码:
hi all,
am new in javascript.
i have used to develop one asp.net application.
here i have make it used one validation process using java script.
i need to validate my email address.
if my email address is valid, i have redirect to another page.
if my email address is not valid, am getting invalid mail id.
but my problem is,
if my email address is valid, i can't able to redirect to another page. please find the code below
and let me know where am made mistake or else give me a valid code.
my code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function validEmail()
{
var mail = document.getElementById('email').value; // where `mail` is id of your input form
var email = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if (mail.match(email))
{
window.location="Default3.aspx";
}
else
{
alert("Invalid Email !!!");
return false;
}
return true;
}
</script>
</head>
<body>
<form id="form1" name="form1" action="" method="post" runat="server">
<div>
<input type="text" name="email" id="email">
<asp:Button ID="Button1" runat="server" OnClientClick="return validEmail()" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
感谢提前。
thanks to advance.
推荐答案
/;
if(mail.match(email))
{
window.location =Default3.aspx;
}
其他
{
alert(无效的电子邮件!!!);
返回false;
}
返回true;
}
< / script >
< / head >
< body >
< 表格 id = form1 名称 = form1 action = 方法 = 发布 runat = server >
< div < span class =code-keyword>>
< 输入 type = text name = email id = 电子邮件 >
< asp:按钮 ID = Button1 runat = server OnClientClick = return validEmail() 正文 = 按钮 OnClick = Button1_Click / >
< / div >
< / form >
< / body < span class =code-keyword>>
< / html >
/; if (mail.match(email)) { window.location="Default3.aspx"; } else { alert("Invalid Email !!!"); return false; } return true; } </script> </head> <body> <form id="form1" name="form1" action="" method="post" runat="server"> <div> <input type="text" name="email" id="email"> <asp:Button ID="Button1" runat="server" OnClientClick="return validEmail()" Text="Button" OnClick="Button1_Click" /> </div> </form> </body> </html>
感谢提前。
thanks to advance.
验证通过后添加return false,以取消button1_click事件。
Add "return false" when the validation passes, so as to cancel the button1_click event.
window.location="Default3.aspx";
return false;
按以下方式更改并查看其是否正常工作
change as per below line and see whether its working or not
if (mail.match(email))
{
this.submit();
}
这篇关于使用javascript验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!