i would like to know how to make my app work the way i want it... at first my app is like this> a user types anything in a textbox and as long as it's a length of 9 or 12 it'll open a form..

here's a small part from my program:

private bool Validate(string Val)
{
if(Val!="")
{
if(Val.Length==9||Val.Length==12)
{
return true;
}
else
return false;
}
else
return false;
}


now i want to make sure that the user either keys in
i) length of 12 numbers eg. 123456123456
OR
ii) a letter that is either s/S/f/F + any 7 numbers + any 1 letter and in this order only eg. F1234567P
to be able to open the form.

here's something i got but donno how to go on from here: (and i donno if it's usable in my program)

input.Length == 9 && System.Text.RegularExpressions.Regex.Match(input, @"[sSfF]\d{7}\w").Success

pls help
thanks

I've never invested the time to learn regular expressions. So I would do it this way for the check of the string of length 9;

private bool isValid9(string input)
		{
			bool result = true;
			if(input == null || input == "") result = false;
			else
			{
				if(input.Length != 9) result = false;
				if(input.Substring(0,1).ToLower() != "f" && input.Substring(0,1).ToLower() != "s")result = false;
				for(int i = 1; i < input.Length -1; i++)
				{
					if(!isNumber(input.Substring(i,1))) result = false;
				}
				if(!isLetter(input.Substring(input.Length-1,1))) result = false;
			}
			return result;
		}
		private bool isNumber(string input)
		{
			string numbers = "0123456789";
			return (numbers.IndexOf(input) > -1);
		}
		private bool isLetter(string input)
		{
			string letters = "abcdefghijklmnopqrstuvwxyz";
			return (letters.IndexOf(input.ToLower()) > -1);
		}

hi

i've tried what u typed n it works! =)
but i have to set another condition that is if 12 DIGITS are entered, condition'll also be set to true..

1) i want to ask you what does Substring(0,1) mean in >>

if(input.Substring(0,1).ToLower() != "f" && 
input.Substring(0,1).ToLower() != "s")result = false; 

and why is there a (0,1)??

2) can you write me the comments for

for(int i = 1; i < input.Length -1; i++)
{
    if(!isNumber(input.Substring(i,1))) result = false;
}
if(!isLetter(input.Substring(input.Length-1,1))) result = false;

because i do not really understand what each line does to the program.

3) does the codes you wrote ensure that the order is followed? that means, is the user supposed to enter in this order only > a letter either s/f followed by any 7 numbers and end with any 1 letter from a-z ? i only want this order to be allowed..

thank you so much!
i was gladly surprised when you replied to my post.. i thought nobody knows how to code.. everyone asks me to use regular expression.. even though i know which kind of regular expression to use now, i still do not know where to insert the codes in my program and so i couldnt proceed.. thanks to you for your timely arrival.. as long as yours can help me, i will forsake regular expression.. luckily i know where to insert your codes, otherwise i donno what to do with my program.. =) thanks!

Edited 3 Years Ago by diafol: fixed formatting

hi

i've tried what u typed n it works! =)
but i have to set another condition that is if 12 DIGITS are entered, condition'll also be set to true..

1) i want to ask you what does Substring(0,1) mean in >>
if(input.Substring(0,1).ToLower() != "f" &&
input.Substring(0,1).ToLower() != "s")result = false;
and why is there a (0,1)??

Substring(x,y) means the part of the string starting at x that is y long.
so:
string junk = "abcdefg";
junk.Substring(0,1) returns "a"
junk.Substring(0,2) returns "ab"
junk.Substring(1,3) returns "bcd"

2) can you write me the comments for
for(int i = 1; i < input.Length -1; i++)
{
if(!isNumber(input.Substring(i,1))) result = false;
}
if(!isLetter(input.Substring(input.Length-1,1))) result = false;
because i do not really understand what each line does to the program.

ok here goes:

for(int i = 1; i < input.Length -1; i++) // start with the 2nd character and go through the second to last character
				{
					if(!isNumber(input.Substring(i,1))) result = false; //if the character is not a number, set result to false
				}

3) does the codes you wrote ensure that the order is followed? that means, is the user supposed to enter in this order only > a letter either s/f followed by any 7 numbers and end with any 1 letter from a-z ? i only want this order to be allowed..

test it and see

as for the validation for the other string. try creating a isvalid12 function similar to the isvalid9 one

This article has been dead for over six months. Start a new discussion instead.