Article

ASP.Net Form Validation Using JavaScript

01 Jun 2015 Kamal Pratap
0 Comments 1710 Views



ASP.Net form validation using JavaScript for alphabets, numeric, email and URL. This code are tested on all browsers without using any JQuery.

Put the javascript code in the head section

JavaScript :

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

        function checkonlyalphabets(a, evt) {
            var charCode = evt.which ? evt.which : evt.keyCode;
            if ((charCode >= 65 && charCode <= 90) || (charCode >= 97 && charCode <= 122))
                return true;
            else {
                alert('Please! Enter Only Alphabets');
                return false;
            }
        }

        function checkonlynumbers(a, evt) {
            var charCode = (evt.which) ? evt.which : evt.keyCode
            if (charCode >= 48 && charCode <= 57)
                return true;
            else {
                alert('Please! Enter Only Numbers');
                return false;
            }
        }

        function validate() {
            var email = document.getElementById('txtEmail').value;
            var emailfilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            var EmailMatch = email.match(emailfilter);
            if (!emailfilter.test(email.value)) {
                if (EmailMatch == null) {
                    alert('Please Insert Correct Email Address.');
                    return false;
                }               
            }

            var url = document.getElementById('txtUrl').value;          
            var urlfilter = /(http(s)?:\\)?([\w-]+\.)+[\w-]+[.com|.in|.org]+(\[\?%&=]*)?/;            
            var UrlMatch = url.match(urlfilter);            
            if (!urlfilter.test(url.value)) {
                if (UrlMatch == null) {
                    alert('Please Insert Correct URL.');
                    return false;
                }              
            }

        }

    </script>

Asp.Net :

  <table>
            <tr>
                <td>
                    Name (Alhabets Only)
                </td>
                <td>
                    <asp:TextBox ID="txtName" runat="server" onKeyPress="return checkonlyalphabets(this, event)" />
                </td>
            </tr>
            <tr>
                <td>
                    Email
                </td>
                <td>
                    <asp:TextBox ID="txtEmail" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    Mobile (Numbers Only)
                </td>
                <td>
                    <asp:TextBox ID="txtMobile" runat="server" onKeyPress="return checkonlynumbers(this, event)" />
                </td>
            </tr>
            <tr>
                <td>
                    URL
                </td>
                <td>
                    <asp:TextBox ID="txtUrl" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Button ID="btnsubmit" runat="server" Text="Save" OnClientClick="return validate();" />
                </td>
            </tr>
        </table>

 

Kamal Pratap

I have 6+ years experience in .Net technologies like Asp.Net, C#, WCF, Web Services, SQL Server, Ajax, LinQ. Currently I am working in Netcarrots Loyalty Services as a Software Developer.

Comments

No coments found to display!

Leave a Comment