Friday 24 May 2013

ASP.Net: Text Box Validation Part II | Neha Sharma




Introduction:

In this article I explain some more validations of Text box control in ASP.Net.

Requirement:

Add the below line in head section to create function for validation

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

Then create a function:

function validate()
{
. //function body
.
.
return true;
}

Solution :

1.       Text box Can not be blank :

This kind of validations used to not to allow blank text boxes control in ASP.Net .Add the below function body :

if (document.getElementById("<%=txtName.ClientID%>").value=="")
      {
                 alert("Name Feild can not be blank");
                 document.getElementById("<%=txtName.ClientID%>").focus();
                 return false;
                
      }

Where txtName=Id of a Text Box control

2.       Email id Validation :

This kind of validation used to accept only email id syntax in a text box control in asp.net.Add the below function body :

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;
    }

Where txtEmail = id of Text Box

3.       Web URL :

This kind of validations used to accept only web url in Text box control .Add the below  function body :

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;
     }
Where txtWebURL = id of Text Box.

4.       ZIP Format :

This kind of validation used to accept only ZIP format of data in the Text Box control in asp.net. add the below function body :

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;
               }
     }

Where txtZIP = id of text box.

When you done with all call the function validate on the Button on which you want output like below :

<asp:Button ID="btnSubmit" OnClientClick=" return validate()" runat="server" Text="Submit" />


Now save the whole work and view the page in browser it ll work perfect.



Thanks


No comments:

Post a Comment

Which Game Engine you should choose | Neha Sharma

Unity3D and Unreal Engine are only 2 Game engine which executes an indispensable position in Game Industry. The choice of the engine must ...