Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple regular expression question

Status
Not open for further replies.

smsinger3

Programmer
Oct 5, 2000
192
US
I need to validate a number this is 8 digits and no more. I use this validation expression:

^[\d]{8}

However, I can still type in more than 8 digits and it returns a true. How can I change it to return a false if more than 8 digits are entered?

Thank you for your help!

SteveS
stevensinger123-tt@yahoo.com
 
Steve:
Give this a shot ^\d{8}$ the dollar sign is the end of the string.
Marty
 
Intersting!
Is it a regular expression validator for a field on a ascx or aspx or is it in the codebehind? I am at a disadvantage here at work because we are not doing .NET and I have no real ide to drop in the rev. This is what I wrote to test rexex's. Without telling the regex that it's the end of the string with the $ it will allow 123456789 to be true.

I miss writing cgi scripts using PERL. Have to make a living!
Marty


using System;
using System.Text.RegularExpressions;

namespace regextest8 // Defining Namespace
{
class MainClass // Defining Class
{
public static void Main(string[] args) // Program Entry Point
{
string _input;
Console.Write("Enter string: ");
_input = Console.ReadLine();
while (!(_input.Equals("end")))
{
MainClass.mymatch(_input);
Console.Write("Enter string: ");
_input = Console.ReadLine();
}
}
public static void mymatch(string _input)
{
Match MyRegMatch = Regex.Match(_input,"^\\d{8}$");
if(MyRegMatch.Success)
{
Console.WriteLine("good string");
}
else
{
Console.WriteLine("bad string");
}
}//end match
}
}
 
This was in a regular expression validation. This is what I used: ^\d{8}$

It works like charm :)

SteveS
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top