Introduction: Sometime
it is required to check the value entered in textbox is string (Alphabets only)
or not. In this article i will explain how to check whether entered value is string (Alphabets only) or not in asp.net using regular expression in both C# and VB languages.
We can check value by using regular expression in
server side code or we can also check data type by using compare validator (Asp.NET
validator) which client side and will not work in case of Javascript disabled.
In this article I will use server side code to check
the data type of the input value.
Asp.Net C# Section
Design Section:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form
id="form1"
runat="server">
<div>
<div>
<fieldset
style="width:350px;">
<legend><strong>Check Input Value is String or not</strong></legend>
<center>
<table>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
Enter Value</td>
<td>
<asp:TextBox ID="txtTest"
runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnTest" runat="server" onclick="btnTest_Click"
Text="Test String" /><br />
<asp:Label ID="lblStatus" runat="server"></asp:Label>
</td>
<td>
</td>
</tr>
</table>
</center>
</fieldset>
</div>
</div>
Write the
code as:
protected void btnTest_Click(object
sender, EventArgs e)
{
if
(IsNumeric(txtTest.Text.Trim()))
{
lblStatus.Text = txtTest.Text + " is String";
lblStatus.ForeColor =
System.Drawing.Color.Green;
}
else
{
lblStatus.Text = txtTest.Text + " is not String";
lblStatus.ForeColor =
System.Drawing.Color.Green;
}
}
System.Text.RegularExpressions.Regex
reg = new System.Text.RegularExpressions.Regex("^[a-zA-Z
]*$");
public bool
IsNumeric(String str)
{
try
{
if
(string.IsNullOrEmpty(str))
{
return
false;
}
if
(!reg.IsMatch(str))
{
return
false;
}
}
catch (Exception ex)
{
lblStatus.Text = "Error occured : " +
ex.Message.ToString();
lblStatus.ForeColor =
System.Drawing.Color.Red;
}
return true;
}
}
Asp.Net VB section:
Design Section:
<div>
<fieldset style="width:200px;">
<legend>Check String is numeric or not</legend>
<center>
<table>
<tr>
<td>
<asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnTest" runat="server" Text="Test String" /><br />
<asp:Label ID="lblStatus" runat="server"></asp:Label>
</td>
</tr>
</table>
</center>
</fieldset>
</div>
write the
code as:
Protected Sub btnTest_Click(sender As Object, e As System.EventArgs) Handles btnTest.Click
If IsNumeric(txtTest.Text.Trim()) Then
lblStatus.Text = txtTest.Text + " is string"
lblStatus.ForeColor = System.Drawing.Color.Green
Else
lblStatus.Text = txtTest.Text + " is
not string"
lblStatus.ForeColor = System.Drawing.Color.Green
End If
End Sub
Private reg As New System.Text.RegularExpressions.Regex("^[0-9]+$")
Public Function IsNumeric(str As [String]) As Boolean
Try
If String.IsNullOrEmpty(str) Then
Return False
End If
If Not reg.IsMatch(str) Then
Return False
End If
Catch ex As Exception
lblStatus.Text = "Error
occured : " &
ex.Message.ToString()
lblStatus.ForeColor = System.Drawing.Color.Red
End Try
Return False
End Function
0 comments:
Post a Comment