Thursday, February 12, 2015

How to validate TextBox inside Gridview using JavaScript

How to validate TextBox inside Gridview using JavaScript

Validating TextBox or any other input control using JavaScript is one of the very common task we do in your ASP.NET application . What if we have to validate the TextBox inside the Gridview using JavaScript? Here we are going to see How to validate the TextBoxes inside the Gridview using JavaScript.
 First we will write the JavaScript to validate the Textbox. This JavaScript will be placed just below the head tag or just above the body tag in your .aspx page.

Below is the simple JavaScript which does Numeric Check.

<script type="text/javascript" language=javascript>
function CheckNumeric()
{
var key;
if(navigator.appName == 'Microsoft Internet Explorer')
key = event.keyCode;
else
key = event.which
if ( !(key >= 48 && key <= 57) && key != 8 && key != 46 && key != 36 && key != 37)
{
event.returnValue = false;
}
}
</script>

Now we will see how to call above JavaScript function. We will call above JavaScript function to check the numeric onkeypress event of the TextBox. If you validate the TextBox onkeypress user will not be able to enter non numeric values.

You will be calling the above function on onkeypress event of the textbox like below.

[code]
//Other Gridview code
...
...
<itemtemplate>
<asp:TextBox id="txtPhone" text='<% # eval("Phone") %>' runat="server" onkeypress="CheckNumeric()"></asp:TextBox>
</itemtemplate>
[/code]
I hope now you are clear about the JavaScript validation on TextBox inside the Gridview

 

No comments:

Post a Comment