how to validate a textbox not to enter character in windows application

Posted by Shanky11 under C# on 3/1/2013 | Points: 10 | Views : 1292 | Status : [Member] | Replies : 2
i have a text box
i want that user can't enter character in txtbx
how can i write code for this




Responses

Posted by: Sakthi.Singaravel on: 3/1/2013 [Member] Silver | Points: 25

Up
0
Down
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

For i = 0 To TextBox1.Text.Length - 1
If IsNumeric(TextBox1.Text.Chars(i)) Then
Else
TextBox1.Text = TextBox1.Text.Remove(i)
MsgBox("Enter Number Only", vbCritical)
Exit For
End If
Next
End Sub


Regards,
Singaravel M

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 3/1/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Window app

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Number_Character_validate_Csharp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
{
e.Handled = true;
}

}
}
}


Web appp
Method-1
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Enter_only_numbers_in_textbox._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>
<script type="text/javascript" language="javascript">
function numeric(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if(charCode > 31 && ((charCode >= 48 && charCode <= 57) || charCode == 46))
return true;
else
{
alert('Please Enter Numeric values.');
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" onkeypress="return numeric(event)"></asp:TextBox>
</div>
</form>
</body>
</html>

Method-2
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Enter_only_numbers_in_textbox.WebForm1" %>
<!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>
<script type="text/javascript" language="javascript">
function numeric( e )
{
var unicode = e.charCode ? e.charCode : e.keyCode;
if( unicode == 8 || unicode == 9 || ( unicode >= 48 && unicode <= 57 ) )
{
return true;
}
else
{
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" name="textbox1" id="textbox1" onkeypress="return numeric(event);" />
</div>
</form>
</body>
</html>


If this post helps you mark it as answer
Thanks

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response