ASP.Net: Text Box Validation - Part II

ASP.Net: Text Box Validation Part I

Introduction

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

Requirement

Add the following line in the head section to create a function for validation.

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

Then create a function:

 

  1. function validate()  
  2. {  
  3. //function body  
  4. .  
  5. .  
  6. return true;  
  7. }  

 

Solution

  1. Text box cannot be blank :

    This kind of validation does not allow blank text box controls in ASP.Net. Add the following function body:

     

    1. if (document.getElementById("<%=txtName.ClientID%>").value=="")  
    2. {  
    3.      alert("Name Feild can not be blank");  
    4.      document.getElementById("<%=txtName.ClientID%>").focus();  
    5.      returnfalse;  
    6. }  

    Where txtName is the Id of a Text Box control. 

  2. Email id Validation

    This kind of validation accepts only email id syntax in a text box control in ASP.Net. Add the following function body:

     

    1. if(document.getElementById("<%=txtEmail.ClientID %>").value=="")  
    2. {  
    3. alert("Email id can not be blank");  
    4. document.getElementById("<%=txtEmail.ClientID %>").focus();  
    5. returnfalse;  
    6. }  
    7. var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;  
    8. var emailid=document.getElementById("<%=txtEmail.ClientID %>").value;  
    9. var matchArray = emailid.match(emailPat);  
    10. if (matchArray == null)  
    11. {  
    12. alert("Your email address seems incorrect. Please try again.");  
    13. document.getElementById("<%=txtEmail.ClientID %>").focus();  
    14. returnfalse;  
    15. }  

    Where txtEmail is the Id of a Text Box.

  3. Web URL

    This kind of validation only accepts a web URL in a Text box control. Add the following function body:

    1. if(document.getElementById("<%=txtWebURL.ClientID %>").value=="")  
    2. {  
    3.             alert("Web URL can not be blank");  
    4.             document.getElementById("<%=txtWebURL.ClientID %>").value="http://"  
    5.             document.getElementById("<%=txtWebURL.ClientID %>").focus();  
    6.             returnfalse;  
    7.  }  
    8.  var Url="^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"  
    9.  var tempURL=document.getElementById("<%=txtWebURL.ClientID%>").value;  
    10.  var matchURL=tempURL.match(Url);  
    11.   if(matchURL==null)  
    12.   {  
    13.           alert("Web URL does not look valid");  
    14.           document.getElementById("<%=txtWebURL.ClientID %>").focus();  
    15.           returnfalse;  
    16.   }  

    Where txtWebURL is the Id of a Text Box.

  4. ZIP Format

    This kind of validation only accepts a ZIP format of data in the Text Box control in ASP.Net. Add the following function body:

     

    1. if (document.getElementById("<%=txtZIP.ClientID%>").value=="")  
    2. {  
    3. alert("Zip Code is not valid");  
    4. document.getElementById("<%=txtZIP.ClientID%>").focus();  
    5. returnfalse;  
    6. }  
    7. var digits="0123456789";  
    8. var temp;  
    9. for (var i=0;i<document.getElementById("<%=txtZIP.ClientID %>").value.length;i++)  
    10. {  
    11. temp=document.getElementById("<%=txtZIP.ClientID%>").value.substring(i,i+1);  
    12. if (digits.indexOf(temp)==-1)  
    13. {  
    14. alert("Please enter correct zip code");  
    15. document.getElementById("<%=txtZIP.ClientID%>").focus();  
    16. returnfalse;  
    17. }  
    18. } 

    Where txtZIP is the Id of a text box.

    When you done with all call the function validate on the Button on which you want output as in the following :

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

Now save the entire work and view the page in a browser. It will work perfectly.