Saturday 19 November 2011

C# Validation for Email,Contact No and AutoText in TextBox



 Regex Validation
1)for email:

public static bool IsValidEmail(string Email)
      {
          return Regex.IsMatch(Email, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
      }


2)for phone no:
ex. 800-555-555

^[2-9]\d{2}-\d{3}-\d{4}$ 




3)Auto text




IN ASPX
  <head runat="server">
    <title></title>
  
    <script type = "text/javascript">
        var defaultText = "Enter your text here";
        function WaterMark(txt, evt) {
            if (txt.value.length == 0 && evt.type == "blur") {
                txt.style.color = "gray";
                txt.value = defaultText;
            }
            if (txt.value == defaultText && evt.type == "focus") {
                txt.style.color = "black";
                txt.value = "";
            }
        }
</script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
  
<asp:TextBox ID="TextBox1" runat="server" Text = "Enter your text here"

    ForeColor = "Gray" onblur = "WaterMark(this, event);"

    onfocus = "WaterMark(this, event);">

</asp:TextBox>

    </div>
    </form>
</body>




IN C#
 protected void Page_Load(object sender, EventArgs e)
    {
        TextBox1.Attributes.Add("onblur", "WaterMark(this, event);");

        TextBox1.Attributes.Add("onfocus", "WaterMark(this, event);");
    }















Only INTEGER/./BACKSPACE Allowed



<script language="javascript" type="text/javascript" >
        function checkForInt(evt) {
            var charCode = (evt.which) ? evt.which : event.keyCode;
            return ((charCode ==08||charCode ==46 || charCode >= 48 && charCode <= 57));
        }
     
</script>







  <asp:TextBox ID="TextBoxMax" runat="server"
                        Font-Names="Tahoma" Font-Size="9pt" Width="150px"  onkeypress="return checkForInt(event)"></asp:TextBox>





charCode ==08 allow you to use backspace too.



No comments:

Post a Comment

Print Only Grid View in ASP.net

ASP.net How to Print Only GridView < div id ="gridviewDiv" >   < asp:GridView ID ="gridViewToPri...