Click here to Skip to main content
15,908,013 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

need help with validating a 9-digit number.
CANNOT BE -----------
000000000
111111111
222222222
333333333
444444444
555555555
666666666
777777777
888888888
999999999

4-5 position(s) CANNOT BE 00 --
123001234

6-9 position(s) CANNOT BE 00 --
234550000

The nine numbers CANNOT BE sequential -- but only the following 4 four below, for the time being --
012345678
123456789
987654321
098765432


I had just managed to get the first piece done --
"^(?:(?!0+|1+|2+|3+|4+))\d{9}$"

Thanks a TON for the help friends.
Posted

Why bust your walnuts on a Regex when you can do this in a few lines of code that would arguably be better optimized and much easier to read?

For example:
C#
if (input.Substring(3, 2) == "00") digitsValid = false;
if (input.Substring(5, 4) == "0000") digitsValid = false;


You need that, a list for invalids, a list for valids and a sequential number check.

For your lists, you could use something like:
C#
List<string> alwaysValid = new List<string> { "012345678", "123456789", "987654321", "098765432" };


You will thank me when you come back in six months and have to modify it, and so will everyone else who would've had to otherwise maintain your code.

Regexes are beautiful but they are not swiss army knives. Whenever you have more than two validation requirements, you're probably best to explore another approach. For instance, Regex is good for your first requirement that you've solved. Great! Use it to prove that out and move on.

Cheers.
 
Share this answer
 
Comments
fjdiewornncalwe 17-Mar-11 18:49pm    
I totally agree. Maintainable code vs. JSL.
I wouldn't use a regex for that, I'd use code.
You could do it with a regular expression, but it would be horribly complex, and very difficult to test and maintain.
The code would be a lot clearer:
Dim digits As String = "012325678"
If ValidString(digits) Then
	Console.WriteLine("OK")
Else
	Console.WriteLine("Invalid")
End If

Private Shared Function ValidString(digits As String) As Boolean
	If digits.Length = 9 Then
		If Not AllSameDigit(digits) Then
			If digits(3) <> "0"C AndAlso digits(4) <> "0"C Then
				If digits(5) <> "0"C AndAlso digits(6) <> "0"C AndAlso digits(7) <> "0"C AndAlso digits(8) <> "0"C Then
					If Not IsSequential(digits) Then
						Return True
					End If
				End If
			End If
		End If
	End If
	Return False
End Function
Private Shared Function IsSequential(digits As String) As Boolean
	Select Case digits
		Case "012345678", "123456789", "987654321", "098765432"
			Return True
	End Select
	Return False
End Function
Private Shared Function AllSameDigit(digits As String) As Boolean
	Dim previous As Char = digits(0)
	For Each c As Char In digits
		If c <> previous Then
			Return False
		End If
	Next
	Return True
End Function
Note that this is untested. But to go from "just these four" to a proper test is a simple mater of changing a single function.
 
Share this answer
 

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