tags:

views:

57

answers:

4

This is what i've come up with so far

private void CheckFormatting()
{
    StringReader objReaderf = new StringReader(txtInput.Text);
    List<String> formatTextList = new List<String>();

     do
     {
         formatTextList.Add(objReaderf.ReadLine());
     } 
     while (objReaderf.Peek() != -1);

     objReaderf.Close();
     for (int i = 0; i < formatTextList.Count; i++)
     {

     } 
}

What it is designed to do is check that the user has entered their information in this format Gxx:xx:xx:xx JGxx where "x" can be any integer.

As you can see the user inputs their data into a multi-line textbox. i then take that data and enter it into a list. the next part is where i'm stuck. i create a for loop to go through the list line by line, but i guess i will also need to go through each line character by character. How do i do this? or is there a faster way of doing it?

thanks in advance

+1  A: 

Regular Expressions is the fast way to do it.

Rice Flour Cookies
I'm quite new to C# so i'm afraid that means nothing to me. sorry. can you elaborate?
Arcadian
Regular Expressions are like a short-hand for text formatting. Any given input either matches a given regular expression, or it doesn't. There are regular expressions for things like dates, social security numbers, etc. .net has some Regular Expression support. See the following link and also try Google:http://en.wikipedia.org/wiki/Regular_expressions
Rice Flour Cookies
+1  A: 

Use a regex In your case, G\d\d:\d\d:\d\d:\d\d:JG\d\d should work (didn't test it) use the using System.Text.RegularExpressions namespace

FBSC
Can you use one in an example so i can tell what it should look like?
Arcadian
Ah, sorry, this is the method:System.Text.RegularExpressions.Regex.IsMatch(your_string, @"G\d\d:\d\d:\d\d:\d\d:JG\d\d");this returns a bool
FBSC
+1  A: 

best practice is to validate user input as they are entering data and make it clear what the format should be in your input design.

you could add a series of text boxes for each numeric section separated by : , and than validate each textbox for numeric values.

i am guessing this is a asp.net Page? if yes, than you can make use of asp.net validators both on the client, and server.

i.e.

<asp:textbox id="textbox1" runat="server"/>
<asp:RegularExpressionValidator id="valRegEx" runat="server"
    ControlToValidate="textbox1"
    ValidationExpression="[0-9]*"
    ErrorMessage="* Your entry is not a valid number."
    display="dynamic">*
</asp:RegularExpressionValidator>
Sonic Soul
Oh yeah, i've made it very clear right above the textbox of the format it should be in, but you know some people just won't read it and then scream and complain to me when it goes wrong, so its nice to have a safety net.No its not asp.net. its a windows based app for storing, updating and viewing data. Its basically an app to speed up data entry.
Arcadian
ok.. i know winforms has its own set of validators, but never used them. you could still apply similar solution.. you could also override OnKeyDown for each box, and reject any non numeric characters..
Sonic Soul
+1  A: 

Try this.

    if (!System.Text.RegularExpressions.Regex.IsMatch("your_text", "G[0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{2} JG[0-9]{2}"))
    {
        //Error!
    }
ok i need to test it but it should look like this then if (Regex.IsMatch(formatTextList[i], "G[0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{2} JG[0-9]{2"));
Arcadian
got a problem, see my edit
Arcadian
What is the exception is thrown? Your method WriteToFile is throwing and exception.
no, the method isn't throwing an exception, i've tested that before. this is a build error. it won't let me use the term "else". if i take it out, then there are no build errors. but i need the "else" part in order for the app to do what i want.
Arcadian
i get invalid expression term "else". but i don't see what invalid about it
Arcadian
nevermind, i sorted it.
Arcadian