Use if-else if or switch- case constructs to check for the right chars. This also seems something that can perfectly be handled by a Regex.
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
I wrote this little console application, hope it helps a bit, I know to little about Regex to give you info about that.
using System;
namespace ConsoleApplication1
{
class Program
{
static int Main(string[] args)
{
// 'A' is an 'illegal' character here
// WriteLine should print false.
string S = "12A3";
Console.WriteLine(CheckString(S));
Console.ReadKey();
return 0;
}
static bool CheckString(string PortStr)
{
bool result = false;
char[] Charray = PortStr.ToCharArray();
for (int i = 0; i < Charray.Length; i++)
{
switch (Charray[i])
{
case '1':
case '2':
case '3':
case '4': result = true;
break;
//etc.
default:
result = false;
return result;
}
}
return result;
}
}
}
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
Hi,
I would seriously recommend checking out some of the many tutorials on Regular Expressions. It is a powerful tool to have under your belt.
Heres a function that will check your data and only allow the valid characters you listed. It will return false if there are double digits(if they arent preceeded by a '/'), non-numerics or any '/'s if not followed by 004 or 005.
Hope this helps :)
private bool ValidInput(string input)
{
const string regExpr = @"[^\d\s]|(\d*?(?<!\/|\/\d)([0-9]{2})|(\/(?!00(4|5))))";
System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(input, regExpr);
if (m.Success)
{
return false;
}
else
{
return true;
}
}
Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
me? adept? naah! lol. I did a little bit of work with reg's a couple years ago, but nothing in depth. I just pieced this together with an online syntax reference and an online tester :p
Bit of trial and error and i was there in no time
Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
Looks like a set of unicode codes, most of them Arabic but not printable?
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
wow..regex with a headache = eviiiiil lol
but i pushed through, i hate to be beaten by a challenge.
Turns out i was comin at it all wrong, the result was pretty straight forward:
//replace
const string regExpr = @"[^\d\s]|(\d*?(?<!\/|\/\d)([0-9]{2})|(\/(?!00(4|5))))";
//with
const string regExpr = @"((?![0-9\s\/]).)|\/(?!00(4|5))";
This will match any character that is not '0-9' a white space or a '/' followed by 004 or 005.
Remember to mark the thread as solved if this has answered your question :D
Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246