C# .NET - javascript to validate list of textbox in gridview

Asked By samaira samaira on 08-May-12 08:22 AM

validate textbox with decimal values using javascript.

GridView with coulmn of TextBox's.
Condition : user can enter only two digits after decimal and maximum of ten digits before decimal.
How do i validate this  using javascript for a column of GridView Texbox's. How do I loop through this
below is the script used:

<script type ="text/javascript">
    function validate(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 (parts[0].length >= 10) return false;
    if (parts.length == 2 && parts[1].length > 2) return false;

Shan P replied to samaira samaira on 08-May-12 11:01 PM
Rather than coding your own validate() function, I'd recommend that you use any number of free 3rd party components that can do this for you on the client side, as they'll be more robust, error free, and cross browser friendly than creating your own solution. If you're able to use jQuery there are a number of components that work well. Are you able to use jQuery in your solution? 

jQuery Format, http://www.examplet.buss.hk/jquery/format.php, is very easy to wire up to any of your text boxes. One thing that it does not support is realtime, inline formatting of your numeric values. For example, auto-rendering a value such as 9000000 within a textbox as 9,000,000. 

jQuery AutoNumeric, http://www.decorplanit.com/plugin/index.htm, is similar to jQuery Format, however, it supports realtime, inline formatting of your numeric values in your text boxes. 

If you're using the ASP.NET Ajax Control Toolkit, the FilteredTextBox is another option -http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/FilteredTextBox/FilteredTextBox.aspx. 

Let me know if any of these meet your need. 
Jitendra Faye replied to samaira samaira on 09-May-12 01:27 AM

Use this code-

Bind this Javascript function to each TextBox in GridView.

LIke this-

protected void GridViewProducts_RowDataBound(object sender, GridViewRowEventArgs e)
if (e.Row .RowType == DataControlRowType .DataRow )
  TextBox txt= (TextBox) e.Row.FindControl("TxtBoxName");

Now use this Javascript function-

function funValidate(ctrl)

var filter = \d{10}+(\.\d{1,2})?;

var test_bool = filter.test(ctrl.value);

if (test_bool == false) {

alert('wrong value');


return false;


TRy this and let me know.
Somesh Yadav replied to samaira samaira on 09-May-12 02:17 AM


try this example

<html xmlns="http://www.w3.org/1999/xhtml">
    <title>Untitled Page</title>
    <script type="text/javascript">
    function onKeyPressBlockNumbers(value)
      reg = /^\-?([1-9]\d*|0)(\.\d?[1-9])?$/;
        alert("please enter numaric values only");
        return false;
      return reg.test(value);
    <input id="Text1" type="text" onpaste="return false;" ondrop="return false"; onchange="onKeyPressBlockNumbers(this.value);" onkeydown="if((event.keyCode!=8)&&(event.keyCode!=190)&&(event.keyCode!=110)){return (/[\d.]/.test(String.fromCharCode(event.keyCode)))}" />
    <input id="Text2" type="text" onpaste="return false;" ondrop="return false"; onkeyup="onKeyPressBlockNumbers(this.value);" />

for more information check this post


Good Luck

Chintan Vaghela replied to samaira samaira on 09-May-12 02:32 AM


Only allowed 10 Digit before and after 2 Decimal Point use following Javascript function.

Using this function you can

-      Allowed maximum only 10 Digit before decimal

-      Allowed 2 digit after decimal

-      Only number and digit allowed

-      Only one dot allowed

You need to call javascript function on Textbox onKeyPress Event



function validate(val) {

        var e = event || evt; // for trans-browser compatibility

        var charCode = e.which || e.keyCode;


        // for only numeric and dot allowed

        if (!(charCode == 46 || (charCode >= 48 && charCode <= 57))) {

          alert("Only numeric and dot allowed");

          return false;


        //for press char value

        var curchar = String.fromCharCode(charCode);


        //concate previous value with current press value

        var mainstring = val + curchar;


        //for only one dot allowed

        if (mainstring.indexOf('.') > -1) {

          if (mainstring.split('.').length > 2) {

            alert("Only one dot allowed");

            return false;



        //for get beforeDecimal value string

        var beforeDecimal = mainstring;

        if (mainstring.indexOf('.') != -1) {

          beforeDecimal = mainstring.substring(0, mainstring.indexOf('.') - 1);


        //for get afterDecimal value string

        var afterDecimal = '';

        if (mainstring.indexOf('.') != -1) {

          afterDecimal = mainstring.substring(mainstring.indexOf('.') + 1, mainstring.length);


        //for check before decimal digit length

        if (beforeDecimal.length > 10) {

          alert("Maximum 10 digit allowed before decimal");

          return false;


        //for check after decimal digit length

        if (afterDecimal.length > 2) {

          alert("Only two decimal places allowed");

          return false;


        return true;




Applied this function into GridView Texbox




            <asp:TextBox ID="txtNumber" runat="server" onKeyPress="return validate(this.value);"></asp:TextBox>





Hope this helpful!





dipa ahuja replied to samaira samaira on 09-May-12 07:26 AM
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    if (e.Row.RowType == DataControlRowType.DataRow)
      TextBox txt1 = (TextBox)e.Row.FindControl("txt1");
      txt1.Attributes.Add("onkeyup""javascript:chk(" + txt1.ClientID + ")");

  <script type="text/javascript">
    function chk(obj) {
      obj.value = obj.value.replace(/[^a-z]/gi, '')
[)ia6l0 iii replied to samaira samaira on 09-May-12 10:17 AM
Please adopt 'Shan's" reply. This is the easiest to maintain as well. 
sikha rani mahapatro replied to Somesh Yadav on 18-Jul-17 01:37 AM
sikha rani mahapatro replied to Chintan Vaghela on 18-Jul-17 01:42 AM
ds not work