Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my reg expression

reg=^[0-9]{10}$ this reg values i need to match with textbox
Posted
Comments
OriginalGriff 26-Jul-12 2:21am    
And?
Your question is?

1 solution

There are many ways to match the expression in textbox:

1) Write Server-side code to match
2) Declare a RegularExpressionValidator for the textbox
3) Use Javascript

1) Server side code:
C#
using System;
using System.Text.RegularExpressions;

    void CheckTextBoxValue()
    {
	// First we see the input string.
	string input = TextBox1.text;
	// Here we call Regex.Match.
	Match match = Regex.Match(input, @"^[0-9]{10}$",
	    RegexOptions.IgnoreCase);
	// Here we check the Match instance.
	if (match.Success)
	{
	    // Finally Write your relevant code here.
	}
    }


2) RegularExpressionValidator: This is an example. Please make your changes accordingly.
ASP.NET
<asp:textbox id="TextBox1" runat="server" validationgroup="check" xmlns:asp="#unknown"></asp:textbox>
<asp:regularexpressionvalidator id="RegularExpressionValidator1" runat="server" controltovalidate="TextBox1" xmlns:asp="#unknown">
ErrorMessage="Please Enter Only Numbers" ValidationExpression="^\d+$" ValidationGroup="check"></asp:regularexpressionvalidator>


3) Using Javascript:
JavaScript
function check(){
var re5digit=/^\d{5}$/ //regular expression defining a 5 digit number
if (document.myform.myinput.value.search(re5digit)==-1) //if match failed
alert("Please enter a valid 5 digit number inside form")
}

HTML
<form name="myform">
<input type="text" name="myinput" size=15>
<input type="button" onClick="check()" value="check">

</form>
 
Share this answer
 
Comments
Sandeep Mewara 26-Jul-12 7:59am    
5! for effort.
Vani Kulkarni 26-Jul-12 8:39am    
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900