locked
Validate the amount of character of a given field in a table. RRS feed

  • Question

  • User662762443 posted

    I have a spreadsheet with certain data, and in this spreadsheet I have the M column named NUCEP, I need to validate the character quantity before performing my import, could anyone suggest me a way to do this?

    obs.:
    NUCEP= mailbox

    I have a system, which import planing containing information related to processes, and in each worksheet I own a field called NUCEP which is not mandatory, however the user fills in anyway, I just wanted to know if there is any code that I can implement for that field in my spreadsheet, just to validate how many characters I have in each row that was typed, I would just like to tailor a code within my project.

    Wednesday, May 30, 2018 9:30 PM

Answers

  • User-330142929 posted

    Hi RtaVix,

    According to your description, I think we could tell the user to enter the correct string by injecting a script into the front end.

    Just like the following code.

           public DataTable GetDatatable()
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("ZIP") });
                dt.Rows.Add(1, "Apple", "123456");
                dt.Rows.Add(2, "Pear", "345454");
                dt.Rows.Add(3, "Grape", "23xg23");
                return dt;
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
                check(GetDatatable());
            }
            public bool check(DataTable dt)
            {
                string ZIPCode = "";
    
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ZIPCode = dt.Rows[i].ItemArray[2].ToString();
                    if (!Regex.IsMatch(ZIPCode, @"^\d{6}$"))
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Please input correct string, Error occurs in Row " + i + "')", true);
                        return false;
                    }
                }
                return true;
            }

    Best Regards

    Abraham

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 13, 2018 2:54 AM

All replies

  • User-330142929 posted

    Hi Rtavix,

    According to your description, I am not clear about your scenario. Could you provide more information about your excel import tool? Or if you just want to validate the email by regular expression, please provide more codes for me to understand your requirements.

    In addition, if you have imported the excel data to a datatable, you could enumerate the datacolumn to achieve you requirements.

    Look forward to your reply.

    Best Regards,

    Abraham

    Monday, June 4, 2018 7:15 AM
  • User662762443 posted

    Maybe I did not express myself correctly with you Abraham, and I apologize for the delay in answering you, because I had serious family problems to solve, well, come on, I need to validate a field in my excel spreadsheet, which is called NUCEP, this NUCEP is like if it was a postal code of a certain city or neighborhood, but I need to perform a validation of number of characters (number) that each sheet of the spreadsheet has, is a validation!

    Wednesday, July 4, 2018 1:17 AM
  • User-330142929 posted

    Hi RtaVix,

    Based on your code, I suggest that we could use a regular expression to verify the postal code.

    In general, the postal code is six numbers, we could use the following code snippets.

    First, fetch the string value.

    string postal = dt.Rows[2].ItemArray[2].ToString();

    And then validate it.

    Return System.Text.RegularExpressions.Regex.IsMatch(postal,@”^\d{6}$”);

    Feel free to let me know if you have any question.

    Best Regards

    Abraham

    Wednesday, July 4, 2018 4:36 AM
  • User662762443 posted

    I have this method that performs the field validations, for the ZIP field, how can I do to validate if there are special characters in my worksheet and inform the user?

    Thursday, July 12, 2018 5:30 PM
  • User-330142929 posted

    Hi RtaVix,

    According to your description, I think we could tell the user to enter the correct string by injecting a script into the front end.

    Just like the following code.

           public DataTable GetDatatable()
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("ZIP") });
                dt.Rows.Add(1, "Apple", "123456");
                dt.Rows.Add(2, "Pear", "345454");
                dt.Rows.Add(3, "Grape", "23xg23");
                return dt;
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
                check(GetDatatable());
            }
            public bool check(DataTable dt)
            {
                string ZIPCode = "";
    
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ZIPCode = dt.Rows[i].ItemArray[2].ToString();
                    if (!Regex.IsMatch(ZIPCode, @"^\d{6}$"))
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Please input correct string, Error occurs in Row " + i + "')", true);
                        return false;
                    }
                }
                return true;
            }

    Best Regards

    Abraham

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 13, 2018 2:54 AM