Client Side Validation with JavaScript in ASP.NET
来源:互联网 发布:淘宝网怎么收藏店铺 编辑:程序博客网 时间:2024/05/18 11:47
from http://www.c-sharpcorner.com/UploadFile/purankaushal/103222006013805AM/1.aspx?ArticleID=10c0cce3-e00f-4dad-9ddb-df0c669afd6c
This simple program will guide how to do client side validation of Form in JavaScript.
In this just make a form as follows:
- Name : <asp:TextBox ID="txtName" />
- Email : <asp:TextBox ID="txtEmail" />
- Web URL : <asp:TextBox ID="txtWebUrl" />
- Zip : <asp:TextBox ID="txtZip" />
-
<
asp:Button ID="btnSubmit" OnClientClick=" return validate()" runat="server" Text="Submit" />
Now on the source code of this form in script tag write the following code:
<
script language="javascript" type="text/javascript">function validate()
{
if (document.getElementById("<%=txtName.ClientID%>").value=="")
{
alert("Name Feild can not be blank");
document.getElementById("<%=txtName.ClientID%>").focus();
return false;
}
if(document.getElementById("<%=txtEmail.ClientID %>").value=="")
{
alert("Email id can not be blank");
document.getElementById("<%=txtEmail.ClientID %>").focus();
return false;
}
var emailPat = /^(/".*/"|[A-Za-z]/w*)@(/[/d{1,3}(/./d{1,3}){3}]|[A-Za-z]/w*(/.[A-Za-z]/w*)+)$/;
var emailid=document.getElementById("<%=txtEmail.ClientID %>").value;
var matchArray = emailid.match(emailPat);
if (matchArray == null)
{
alert("Your email address seems incorrect. Please try again.");
document.getElementById("<%=txtEmail.ClientID %>").focus();
return false;
}
if(document.getElementById("<%=txtWebURL.ClientID %>").value=="")
{
alert("Web URL can not be blank");
document.getElementById("<%=txtWebURL.ClientID %>").value="http://"
document.getElementById("<%=txtWebURL.ClientID %>").focus();
return false;
}
var Url="^[A-Za-z]+://[A-Za-z0-9-_]+//.[A-Za-z0-9-_%&/?//.=]+$"
var tempURL=document.getElementById("<%=txtWebURL.ClientID%>").value;
var matchURL=tempURL.match(Url);
if(matchURL==null)
{
alert("Web URL does not look valid");
document.getElementById("<%=txtWebURL.ClientID %>").focus();
return false;
}
if (document.getElementById("<%=txtZIP.ClientID%>").value=="")
{
alert("Zip Code is not valid");
document.getElementById("<%=txtZIP.ClientID%>").focus();
return false;
}
var digits="0123456789";
var temp;
for (var i=0;i<document.getElementById("<%=txtZIP.ClientID %>").value.length;i++)
{
temp=document.getElementById("<%=txtZIP.ClientID%>").value.substring(i,i+1);
if (digits.indexOf(temp)==-1)
{
alert("Please enter correct zip code");
document.getElementById("<%=txtZIP.ClientID%>").focus();
return false;
}
}
return true;
}
</script>
And in code behind file just write the below code.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
btnSubmit.Attributes.Add("onclick", "return validate()")
End Sub
Now you will get a form with proper validation.
I hope this is going to help you.
- Client Side Validation with JavaScript in ASP.NET
- Part 85 - Enable client side validation in asp.net mvc
- Client Side Validation of GridView control with javascript
- Embed client-side JavaScript in an ASP.NET 1.1 web page from server side code
- Validation expression in ASP.NET
- ASP.NET Validation in Depth
- Client-Side Reporting with Visual Studio in C#
- Server side Message box in ASP.Net
- Add new rows to WebCombo in client-side javascript
- asp.net server side using JavaScript
- Client Side Validation Using the XMLHTTPRequest Object
- About client-side form validation and frameworks
- Client Side Expand/Collapse All Nodes For ASP.NET 2.0
- [转]ASP.NET AJAX client-side framework failed to load
- Validation CheckBoxList in asp.net form
- Upload files in ASP.NET MVC with JavaScript and C#
- ASP.NET Server-Side Charting With OWC11——饼状图
- ASP.NET MVC 3: Server-Side Comments with Razor
- 初步决定考研
- 分页公式以及分页导航栏总结
- 关于MySql字符集问题
- 常用电脑资料速查
- 关于驱动开发的几个必读BLOG
- Client Side Validation with JavaScript in ASP.NET
- ...!@~....!@#....+_#%.
- SOA校园培训摘要!
- Java游戏开发案例-方块游戏
- Hello,World!
- 倒置对象
- C#转换汉字大写函数
- C#下获得MAC地址类
- 搞清了网页标准的几个问题