Please visit my new Web Site https://coderstechzone.com
In most cases asp.net C# VB.Net developers need to check or validate user input for integer, decimal & DateTime. Asp.net C# provides us an easy way to validate those user inputs from server side. Here in this small article i will show you how one can validate using Asp.Net. Basically TryParse boolean returntype method will help us to check user inputs. It Converts the specified string representation of a Date and time / Integer / Decimal to its DateTime equivalent Integer equivalent Decimal equivalent and returns a value that indicates whether the conversion succeeded or not. In the below example i will show you to validate DateTime, Integer & Decimal values validation.
The output like:
To do that add a page in your project & paste the below HTML MARKUP:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="validate_int_datetime.aspx.cs" Inherits="validate_int_datetime" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>How to validate integer double numeric datetime in asp.net</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="lbl_integer" runat="server" Text="Enter Integer: "></asp:Label> <asp:TextBox runat="server" ID="txt_integer"></asp:TextBox> <asp:Button ID="cmd_integer" runat="server" Text="Click to check" OnClick="cmd_integer_Click" /> <asp:Label ID="lbl_integer_result" runat="server" Font-Bold="True"></asp:Label> <br /> <hr /> <br /> <asp:Label ID="lbl_decimal" runat="server" Text="Enter Decimal: "></asp:Label> <asp:TextBox runat="server" ID="txt_decimal"></asp:TextBox> <asp:Button ID="cmd_decimal" runat="server" Text="Click to check" OnClick="cmd_decimal_Click" /> <asp:Label ID="lbl_decimal_result" runat="server" Font-Bold="True"></asp:Label> <br /> <hr /> <br /> <asp:Label ID="lbl_Datetime" runat="server" Text="Enter datetime: "></asp:Label> <asp:TextBox runat="server" ID="txt_Datetime"></asp:TextBox> <asp:Button ID="cmd_Datetime" runat="server" Text="Click to check" OnClick="cmd_Datetime_Click" /> <asp:Label ID="lbl_datetime_result" runat="server" Font-Bold="True"></asp:Label> </div> </form> </body> </html>Now in codebehind write the below code:
using System; public partial class validate_int_datetime : System.Web.UI.Page { Int32 Check_Integer; decimal check_decimal; DateTime check_Datetime; protected void Page_Load(object sender, EventArgs e) { } protected void cmd_integer_Click(object sender, EventArgs e) { if(Int32.TryParse(txt_integer.Text,out Check_Integer)) lbl_integer_result.Text="Integer Validation Successfull."; else lbl_integer_result.Text = "Invalid Integer!!"; } protected void cmd_decimal_Click(object sender, EventArgs e) { if (decimal.TryParse(txt_decimal.Text, out check_decimal)) lbl_decimal_result.Text = "Decimal Validation Successfull."; else lbl_decimal_result.Text = "Invalid Decimal!!"; } protected void cmd_Datetime_Click(object sender, EventArgs e) { if (DateTime.TryParse(txt_Datetime.Text, out check_Datetime)) lbl_datetime_result.Text = "Datetime Validation Successfull."; else lbl_datetime_result.Text = "Invalid Datetime!!"; } }Check the code how i use TryParse to validate datatypes.
Hope now you can validate any primitive datattypes using asp.net.
0 comments:
I WOULD BE DELIGHTED TO HEAR FROM YOU