Blue Theme Orange Theme Green Theme Red Theme
 
DevExpress Free UI Controls
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » JavaScript » Client Validations in javascript

Client Validations in javascript

Its shows validations done using pure javascript and it is not using any Validations controls provided by the .NET Framework 3.5.

Page Views : 1164
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Its shows validations done using pure javascript and it is not using any Validations controls provided by the .NET Framework 3.5
 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>

    <script language="javascript" type="text/javascript">

 

          function calendarPicker(strField)

          {

           window.open('DatePicker.aspx?field=' + strField, 'calendarPopup', 'width=250,height=190,resizable=yes');

 

          }

        

          var flOrderNum=false;

          var flOrderDate=false;

          var flDeliveryDate=false;

          var flSupplierName=false;

          var flTotAmt = false;

         

          function DisplayValidations()

          {

              var reason="";

             

              reason += ValidateOrderNumber();

            

              reason += ValidateOrderDate();

              reason += ValidateDeliveryDate();

              reason += ValidateSupplierName();

        

              reason +=  ValidateTotalAmt();

           

           

            if(reason != "")

            {

              alert("Some fields need correction:\n\n" + reason + "\n") ;

           

              if(flOrderNum == true)

              {

           

               document.getElementById("txtOrderNumber").focus();

               flOrderNum=false;

               return false;

                 

              }

                          

            if(flOrderDate == true)

              {

               document.getElementById("pickOrderDate").focus();

               flOrderDate=false;

               return false;

              }

             

            if(flDeliveryDate == true)

             {

                 document.getElementById("pickDeliveryDate").focus();

                 flDeliveryDate=false;

                 return false;

             }

         

            if (flSupplierName == true)

             {

                 document.getElementById("dropSupplier").focus();

                 flSupplierName=false;

                 return false;

              

             }

        

            if(flTotAmt == true)

             {

                document.getElementById("txtTotalAmt").focus();

                flTotAmt=false;

                return false;

            

             }

         

          

           //  return false;

 

            }

             return true;

          }

         

          function ValidateOrderNumber()

          {

            var error="";

            var alphaExp = "o{1}[0-9]{5}";

            if(document.getElementById("txtOrderNumber").value.length==0)

            {

              error="Please enter the OrderNumber \n";

                  flOrderNum=true;         

        }else if(document.getElementById("txtOrderNumber").value.length != 6)

              {

               error="Wrong length of OrderNumber \n";

             

               flOrderNum=true;

              }             

              else if(validatereg()==false )

              {

                error="Wrong OrderNumber";

                 flOrderNum=true;

              }

            //alert(flOrderNum);

             

             return error;

            

          }

         

          function ValidateSupplierName()

          {

            var errSupplier="";

            if (document.getElementById("dropSupplier").value == 0)

            {

              errSupplier="\n Please select the Supplier";

              flSupplierName=true;

             

            }

            return errSupplier;

          }

      

         

  

         

          function ValidateOrderDate()

          {

            var errorOrder="";

            var Edate;

            Edate=new Date(document.getElementById("txtOrderDate").value); 

           

            var d=new Date();

           if(document.getElementById("txtOrderDate").value.length == 0)

            {

              errorOrder="\n Please enter the Order Date ";

              flOrderDate=true;      

            }

           else if(Edate.toDateString()!= d.toDateString() )

             {

              errorOrder= "\n Order Date is not Today's Date";

              flOrderDate=true;

             }

            

           return errorOrder;

         

              

         }

        

         function ValidateDeliveryDate()

         {

           var errorDelivery="";

           var Entereddate;

           Entereddate=new Date(document.getElementById("txtDeliveryDate").value); 

           

           var d=new Date();

           if(document.getElementById("txtDeliveryDate").value.length == 0)

           {

             errorDelivery="\n Please Enter the Delivery Date";    

             flDeliveryDate=true;

            }

                   else if(Entereddate.toDateString() < d.toDateString() )

           {

            

     errorDelivery= "\n Delivery Date should be greater than or equal to Today's Date";

             flDeliveryDate=true;

            

           }

         

         return errorDelivery;

               

         }

         

                

                 function ValidateTotalAmt()

                 {

                 var error="";

                 var totAmt=document.getElementById("txtTotalAmt").value ;

                 if(totAmt.length == 0)

                 {

                   error= "\n Please Enter the Totol Amount ";

                   flTotAmt=true;

                 }

                 else

                 {  if(totAmt>0 && totAmt < 9999.99)

                   {

                   }

                   else

                   { flTotAmt=true;

                     error="\n Please enter a valid Total Amount";

                    }

                  }

                 

                  return error;

                 }

         

         

         

          function validatereg()

          {

           var myRegExp = new RegExp("o{1}[0-9]{5}", "i")

           var str1=document.getElementById("txtOrderNumber").value;

       

           var result=myRegExp.test(str1);

           return result;

                   

          }

         

         

         

</script>

 

 

</head>

<body>

    <form id="form1" runat="server">

    <div>

   

        <br />

        <br />

&nbsp; OrderNumber&nbsp;

        <asp:TextBox ID="txtOrderNumber" runat="server" MaxLength="6"></asp:TextBox>

        <br />

      &nbsp; OrderDate&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;

        <asp:TextBox ID="txtOrderDate" runat="server" ReadOnly="True"></asp:TextBox>

        <a href="javascript:;" id="pickOrderDate" onclick="calendarPicker('form1.txtOrderDate');" title="Pick Date from Calendar">pick</a>

        <br />

&nbsp;

        <br />

&nbsp; Delivery Date&nbsp;

        <asp:TextBox ID="txtDeliveryDate" runat="server" ReadOnly="True"></asp:TextBox>

          <a href="javascript:;" id="pickDeliveryDate" onclick="calendarPicker('form1.txtDeliveryDate');" title="Pick Date from Calendar">pick</a>

     

        <br />

        <br />

&nbsp;Supplier Name

        <asp:DropDownList ID="dropSupplier" runat="server">

            <asp:ListItem Selected="True" Value="-1">Select Supplier</asp:ListItem>

        </asp:DropDownList>

        <br />

        <br />

&nbsp;Total Amount&nbsp;&nbsp;

        <asp:TextBox ID="txtTotalAmt" runat="server"></asp:TextBox>

        <br />

        <br />

        <br />

 

        <asp:Button ID="Button1" runat="server" Text="Submit"  />

&nbsp;

        <asp:Button ID="Button2" runat="server" Text="Clear" OnClientClick="form1.reset(); return false;" />

     

        <br />

&nbsp;</div>

    </form>

</body>

</html>

 

 

using System.Xml.Linq;

using System.Data.SqlClient;

 

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

        if (!IsPostBack)

        {

            dropSupplier.Items.Add("Select Supplier:");

 

            String strconn;

            SqlCommand sqlcmd = new SqlCommand();

            SqlDataReader oReader;

            strconn = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;

            SqlConnection oconn=new SqlConnection(strconn);

            oconn.Open();

        sqlcmd = new SqlCommand("select SupplierName from Supplier", oconn);

            oReader = sqlcmd.ExecuteReader();

      

           dropSupplier.DataSource = oReader;

              

           dropSupplier.DataTextField = "SupplierName";

              //  dropSupplier.DataValueField = "SupplierId";

          dropSupplier.DataBind();

 

        dropSupplier.Items.Insert(0, new ListItem("Select Supplier"string.Empty));

 

         Button1.Attributes.Add("OnClick", "DisplayValidations(); return false");

 

 

        }

 

    }

 

 

    protected void Page_PreRender(object sender, EventArgs e)

    {

        ScriptManager.RegisterStartupScript(this, typeof(DropDownList), "focusOnDDL", "document.getElementById('dropSupplier').focus();", true);

    }

 

}

 

 

Below is the Calendar control :-

 

 <%@ Page Language="C#" AutoEventWireup="false" CodeFile="DatePicker.aspx.cs" Inherits="DatePicker" %>

 

<!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>

</head>

<body>

    <form id="form1" runat="server" method="post" >

    <div>

   

    </div>

    <asp:Calendar ID="Calendar1"  runat="server"  BackColor="#FFFFCC"

        BorderColor="#FFCC66" BorderWidth="1px" DayNameFormat="Shortest"

        Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399" Height="200px"

        ShowGridLines="True"

        Width="220px" >

        <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />

        <SelectorStyle BackColor="#FFCC66" />

        <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />

        <OtherMonthDayStyle ForeColor="#CC9966" />

        <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />

        <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />

        <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt"

            ForeColor="#FFFFCC" />

    </asp:Calendar>

    </form>

</body>
</
html>

using System;
using
System.Collections;
using
System.Configuration;
using
System.Data;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;

 public partial  class DatePicker : System.Web.UI.Page
{
    public void Page_Load(object sender, EventArgs e)
    {

    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        Calendar1.DayRender += new DayRenderEventHandler(Calendar1_DayRender);

    }

 private void Calendar1_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
{         
// Clear the link from this day

             e.Cell.Controls.Clear();

            // Add the custom link

             System.Web.UI.HtmlControls.HtmlGenericControl Link = new System.Web.UI.HtmlControls.HtmlGenericControl();

            Link.TagName = "a";

            Link.InnerText = e.Day.DayNumberText;

            Link.Attributes.Add("href", String.Format("JavaScript:window.opener.document.{0}.value = \'{1:d}\'; window.close();", Request.QueryString["field"], e.Day.Date));

             // By default, this will highlight today's date.
            if(e.Day.IsSelected)
            {
              Link.Attributes.Add("style", this.Calendar1.SelectedDayStyle.ToString());

            }

             // Now add our custom link to the page
            e.Cell.Controls.Add(Link);
}

 }
 

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Swati
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Mindcracker MVP Summit 2012
Become a Sponsor
 Comments
please send me asp.net articles by subrahamanyam On December 20, 2011
excellent
Reply | Email | Modify 
Mindcracker MVP Summit 2012
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.