Friday, 13 July 2012

Textbox validation to accept only integer / Only integer with fixed length /only character or show in a validation summery

 //Only Take Integer Value through out the page in that control which textbox not take an integer value

 <script type="text/javascript">
        function numeric_only(e) {
            var unicode = e.charCode ? e.charCode : e.keyCode;
            if (unicode == 8 || unicode == 9 || (unicode >= 48 && unicode <= 57)) {
                return true;
            }
            else {
                return false;
            }
        }
    </script>
<asp:TextBox ID="TxtNextPurchaseOrderNumber" runat="server" onkeypress="return numeric_only(event);"/> 


//Only Take Integer Value
<script type="text/javascript">
    function validate(s) {
        var x = s.keyCode;
        var Zip = document.getElementById('<%=TxtRoundupPlace.ClientID %>');
        var j = "0123456789"
        for (var i = 0; i < Zip.value.length; i++) {
            if (j.search(Zip.value.charAt(i)) == -1) {
                alert('Round Place should be Numeric ');
                Zip.value = "";
                return false;
            }
        }
    }
</script>
//Event write in this format
   <asp:TextBox ID="TxtRoundupPlace" Text="" runat="server" onkeyUp="return validate(event)" />
______________________________________________________________________________
if i want  one TextBox take only integer value and  6 digits number thats way
    <script type="text/javascript">

        function validate(s)
         {
             var x = s.keyCode;
             var Zip = document.getElementById('<%=
TxtRoundupPlace.ClientID %>');
             var j = "0123456789"
             for (var i = 0; i < Zip.value.length; i++) {

                 if (j.search(Zip.value.charAt(i)) == -1) {
                     alert('Zip code  should be Numeric ');
                     Zip.value = "";
                     return false;
                 }
             }

             if (Zip.value.length >= 7)
            {
                alert('Zip code lenght must be less than Seven');
                Zip.value = "";
                return false;
            }
    </script>
   <asp:TextBox ID="TxtRoundupPlace" Text="" runat="server" onkeyUp="return validate(event)" />
 ___________________________________________________________________________
//Only Take Character or String Value
<script type="text/javascript">
    function AllowAlphabet(e) {
        isIE = document.all ? 1 : 0
        keyEntry = !isIE ? e.which : event.keyCode;
        if (((keyEntry >= '65') && (keyEntry <= '90')) || ((keyEntry >= '97') && (keyEntry <= '122')) || (keyEntry == '46') || (keyEntry == '32') || keyEntry == '45')
            return true;
        else {
            alert("Please Enter Only Character values.");
            return false;
        }
    }
</script>
//Event write in this format 
      <asp:TextBox ID="TxtCurrencySubString" Text="" runat="server" onkeypress="return AllowAlphabet(event)" /> 
____________________________________________________________________________
TextBox Take only two digit decimal places after number using Java script  
<script type ="text/javascript">
    function MaskMoney(evt)
    {
    if (!(evt.keyCode == 46 || (evt.keyCode >= 48 && evt.keyCode <= 57)))
return false;
    var parts = evt.srcElement.value.split('.');
    if (parts.length > 2) return false;
    if (evt.keyCode == 46) return (parts.length == 1);
    if (parts[0].length >= 14) return false;
    if (parts.length == 2 && parts[1].length >= 2) return false;
    }
</script>

filename.aspx.cs("onKeyPress)

protected void Page_Load(object sender, EventArgs e)
        {
           TextBox1.Attributes.Add("onKeypress", "return MaskMoney(event)");
        }
[Or]
filename.aspx.cs("onKeyDown)
protected void Page_Load(object sender, EventArgs e)
        {
           TextBox1.Attributes.Add("onKeydown", "return MaskMoney(event)");
        }
_______________________________________________________________________________
 //This Red Color is allow to a textbox only String Value (1,10) means 10 Character
<asp:TextBox ID="TxtCurrencySymbol" Text="" runat="server"  />
              <asp:RegularExpressionValidator ID="RegCurrencySymbol" runat="server"
                ControlToValidate="TxtCurrencySymbol" ValidationExpression="^[a-zA-Z'.\s]{1,10}$"
                ErrorMessage="Currency Symbol Only Characters !"  Display="None">*</asp:RegularExpressionValidator>
 //This Red Color is allow to a textbox only IntegerValue
   <asp:RegularExpressionValidator ID="regRoundPlace" runat="server" ControlToValidate="TxtRoundupPlace"
                ValidationExpression="[0-9]+"  ErrorMessage="Roundup Place Should be numeric value !" Display="None">*</asp:RegularExpressionValidator> 

//This message is show in a Validation Summery
 asp:ValidationSummary ID="ValidationAllMessage" runat="server" ShowMessageBox="true" ShowSummary="false"  />

No comments:

Post a Comment