Click here to Skip to main content
15,881,938 members
Articles / Style
Article

AJAX Required Field Validator

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL1 min read 11.3K   1  
Ajax ValidatiorIn this Article I'll Show you How to call a RequiredFieldValidator With the aid of AJAX.Step1:- In this case,there are three controls

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Ajax Validatior


In this Article I'll Show you How to call a RequiredFieldValidator With the aid of AJAX.


Step1:- In this case,there are three controls in .aspx page

1.TextBox(txtfname)
2.Label (lblmsg) :-To show the Error Message.
3.and a Button(btnshow
 
 
 
 <asp:TextBox ID="txtfname" runat="server"></asp:TextBox>
 <asp:Label ID="lblmsg" runat="server" Text="Please Enter your First Name" Style="color: #ff0000; display:none;"></asp:Label>
   <input id="btnshow" type="button" onclick="ShowValdidator()" value="Enter" />



Note:-  Here We set the style of Label(lblmsg) :- display:none :-means the label is not display.


Here We call the function ShowValdidator() on the onclick event of btnshow,by this we check the TextBox value and call the Label(error meassage)




Step2: Call Function (ShowValdidator()):

In the <head> part write the folowing code:-


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

var xmlHttp

var arr;

function ShowValdidator()

{


xmlHttp=GetXmlHttpObject()

var url="Default.aspx"

url=url+"?fname="+document.getElementById('txtfname').value

xmlHttp.onreadystatechange=stateChanged 

xmlHttp.open("GET",url,true)

xmlHttp.send(null)

return false;

}

function stateChanged() 


if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")


      var str;

      str = xmlHttp.responseText;

      if(str=="")
      {
        document.getElementById("lblmsg").style.display='';

      }
      else
      {
         document.getElementById("lblmsg").style.display='none';
      }
      
}

}      

function GetXmlHttpObject()


var objXMLHttp=null

if (window.XMLHttpRequest)

{

objXMLHttp=new XMLHttpRequest()

}

else if (window.ActiveXObject)

{

objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")

}

return objXMLHttp


 </script>





Step 3:- In behind code (C#) :-


protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["fname"] != null)
            {

                string fname = Request.QueryString["fname"];

                Response.Clear();

                string str = fname.ToString();
                Response.Write(str);

                Response.End();

            }
        }
    }


     





License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --