Powered by Blogger.

Thursday, February 27, 2014

How to validate user input to accept only 10 character using Regulat expressions in Javascript?



In this post we will discuss about how to validate user input to accept only 10 character using Regulat expressions in Javascript?

Also check out:

- Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements

- Common Language Runtime in C#.Net

- How to Count number of times website visited and online users in asp.net using C#.net?

The below  Html code explains the validation for name which accepts only lower case and upper case alphabets using Regular Expressions.

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
    <script language="ecmascript" type="text/ecmascript">
        function validateEmpName() {
               
            var name = document.getElementById(" <%=empName.ClientID%>").value;
           // Reg Exp to accept only A-Z and a-z
            var p1 = /^([A-z])+$/;
            var x1 = name.match(p1);
            if (x1 == null) {
                document.getElementById("empName").style.color = "red";  
                alert("Employee name is not in the corrrect format");
                document.getElementById("<%=empName.ClientID%>").focus;
                return false;
            }
            return true;
          }
     </script>
 </head><body>
    <form id="form1" runat="server">
    <div>
         Enter Employee Name:
        <asp:TextBox ID="empName"  runat="server" Width="132px"></asp:TextBox>
         <asp:Button ID="Button1" runat="server" Text="Check Name"
            OnClientClick = "return validateEmpName();"   />
    </div>
    </form>
</body>
</html>