Introduction
I will explain how to validate form using JavaScript validations in asp.net
Description
I have sample textbox in which we need to input Price and we need to restrict the user to restrict the input of characters.
If I want to check whether user enters data in textbox or not write script like this
Javascript code
function isNumberKey(evt) {
var Charcode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode 57))
return false;
return true;
}
Asp.Net Code
Page Code
@ Page Language="C#" AutoEventWireup="true" CodeFile="Demo1.aspx.cs" Inherits="Demo1" %>
DOCTYPE html>
html xmlns="http://www.w3.org/1999/xhtml">
head runat="server">
title>title>
script type="text/javascript">
function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode 57))
return false;
return true;
}
script>
head>
body>
form id="form1" runat="server">
div>
table style="width:100%;">
tr>
td>Pricetd>
td>
asp:TextBox ID="txtPrice" runat="server" onkeypress="return isNumberKey(event)" Text="0" Width="219px">asp:TextBox>
td>
td> td>
tr>
table>
div>
form>
body>
html>