How to validate User inputs

Avicool08
Posted by in ASP.NET category on for Beginner level | Views : 2600 red flag

This is a simple tip to validate user's input, restricting any dagerious html tagas entering etc.


 Download source code for How to validate User inputs

Server.HtmlEncode(txtName.Text));

This code will encode user's input, and it is safe to store into the database.

Example:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

</div>

</form>

</body>

</html>

 

 

protected void Page_Load(object sender, EventArgs e)

{

}

protected void Button1_Click(object sender, EventArgs e)

{

string str = Server.HtmlEncode(TextBox1.Text);//here str is encoded str=123&lt; &gt;&gt;

Response.Write(str);

string str1 = TextBox1.Text;//here str is not encoded str=123< >>

Response.Write(str1);

}

 

 

Page copy protected against web site content infringement by Copyscape

About the Author

Avicool08
Full Name: avinash tumulu
Member Level: Starter
Member Status: Member
Member Since: 10/1/2008 12:12:33 AM
Country: India


Hi every one.... Njoy coding....

Login to vote for this post.

Comments or Responses

Posted by: Bhupender.Yadav on: 4/11/2013 | Points: 25
what you exactly want to do???

Login to post response

Comment using Facebook(Author doesn't get notification)