Client Side Validation with JavaScript in ASP.NET

转载 2006年06月04日 10:45:00

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:

  1. Name : <asp:TextBox ID="txtName" />
  2. Email : <asp:TextBox ID="txtEmail" />
  3. Web URL : <asp:TextBox ID="txtWebUrl" />
  4. Zip : <asp:TextBox ID="txtZip" />
  5. <

    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.

相关文章推荐

[转]ASP.NET AJAX client-side framework failed to load

====================================================== 注:本文源代码点此下载 =============================...

Upload files in ASP.NET MVC with JavaScript and C#

GOOGLE的地址被封了,转过来,方便需要的人 https://cmatskas.com/upload-files-in-asp-net-mvc-with-javascript-and-c/ ...
  • Ani
  • Ani
  • 2016-05-18 21:05
  • 394

ASP.NET Server-Side Charting With OWC11——饼状图

最近主要在研究ASP.NET报表的制作,希望把研究出来的东西,分享出来。一方面希望能给这方面有需求的朋友一些弱弱的思路,另一方面希望有研究不足的地方,望大家能够多多指点。 大家共同进步。    今天做...

Server-Side Paging with the Entity Framework and ASP.NET MVC 3

Julie Lerman Download the Code Sample In my February Data Points column, I showed off the jQuer...

ASP.NET Validation in Depth

http://msdn.microsoft.com/en-us/library/aa479045.aspx#aspplusvalid_serverside Server-Side Valid...

Sample Code on ASP.NET Validation in Depth

Base on my previous post "ASP.NET Validation in Depth" http://blog.csdn.net/riverlau/article/details...

ASP.NET Ajax client-side framework failed to load OR ‘Sys’ is undefined(ASP.NET Ajax 客户端框架未能加载)

部署好网站后运行发现JS脚本错误,提示ASP.NET Ajax client-side framework failed to load,后来到处找原因,就是找不到,从头到尾解决方法如下,最后终于成功...

Host an ASP.NET Site Web in IIS7 with Postgresql

做了.net这么久,终于要做点ASP.NET的东西了。开始这份.NET的工作这么久,其实我.NET一点都没学过,纯自学,基本靠GOOGLE,好现在开始建站,我上司说自己也没做过,好吧,自己弄吧。 首先...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)