Answered by:
email valiation

Question
-
User66371569 posted
I have gridview
id name email vdate 1 mm ms@gmail.com 1/1/2019 2 aa aaaa-gmail.com 3 dd 7/9/2022 what i want to check in button press
if email is not null and valid or vdate null and expired then
example. first one in table mm his email is valid and not null but vdate has expired
error: "vdate is expired for mm"
second one aa his mail not valid and vdate is null
error : email not valid and vdate is empty pleas fix email and vdate for aa"
third one dd
error : " please fill email for dd"
i want to check gridview row by row if first row wrong error appears once i fix it then i check second row
i want it in button press event
thank you
Friday, April 9, 2021 8:06 AM
Answers
-
User-939850651 posted
Hi thepast,
You only need to traverse each row of the GridView in the button event, and return the corresponding result when the situation you describe occurs.
A simple example:
protected void Page_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); if (!Page.IsPostBack) { dt.Columns.AddRange(new DataColumn[] { new DataColumn("id"), new DataColumn("name"), new DataColumn("email"), new DataColumn("vdate") }); dt.Rows.Add(1, "mm", "ms@gmail.com", "1/1/2019"); dt.Rows.Add(2, "aa", "aaaa-gmail.com", string.Empty); dt.Rows.Add(3, "dd", string.Empty, "7/9/2022"); Gv1.DataSource = dt; Gv1.DataBind(); } } protected void checkData_Click(object sender, EventArgs e) { foreach (GridViewRow row in Gv1.Rows) { if (row.RowType == DataControlRowType.DataRow) { string name = row.Cells[1].Text.ToString().Replace(" ",""); string email = row.Cells[2].Text.ToString().Replace(" ", ""); string vdate = row.Cells[3].Text.ToString().Replace(" ", ""); //check if email and vdate empty or valid bool isEmailEmpty = string.IsNullOrEmpty(email); bool isVdateEmpty = string.IsNullOrEmpty(vdate); bool isEmailValid = IsValidEmail(email); DateTime date; if (!isVdateEmpty) { date = DateTime.ParseExact(vdate, "d/M/yyyy", CultureInfo.InvariantCulture); } else { date = DateTime.MinValue; } bool isVdateVaild = (!isVdateEmpty) && (DateTime.Compare(date, DateTime.Now)> 0); if (isEmailValid && !isVdateVaild) {ResultLabel.Text = "vdate is expired for " + name;break; } if (!isEmailValid && isVdateEmpty) {ResultLabel.Text = "email not valid and vdate is empty, fix email and vdate for " + name;break; } if (isEmailEmpty && isVdateVaild) {ResultLabel.Text = "email is empty, please fill email for " + name;break; } } } } static bool IsValidEmail(string email) { try { var addr = new MailAddress(email); return true; } catch { return false; } } protected void updateTB_Click(object sender, EventArgs e) { ResultLabel.Text = ""; DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn("id"), new DataColumn("name"), new DataColumn("email"), new DataColumn("vdate") }); dt.Rows.Add(1, "mm", "ms@gmail.com", "1/1/2023"); dt.Rows.Add(2, "aa", "aaaa-gmail.com", string.Empty); dt.Rows.Add(3, "dd", string.Empty, "7/9/2022"); Gv1.DataSource = dt; Gv1.DataBind(); }
<form id="form1" runat="server"> <div> <asp:GridView runat="server" ID="Gv1" AutoGenerateColumns="true"></asp:GridView> </div> <br /> Result: <asp:Label Text="" ID="ResultLabel" runat="server" /> <br /> <asp:Button Text="Check" ID="checkData" OnClick="checkData_Click" runat="server" /> <asp:Button Text="update" ID="updateTB" OnClick="updateTB_Click" runat="server" /> </form>
Result:
If you need to judge whether the data is empty or valid at the same time, there will be many different results, I think it is necessary to simplify this step. Of course, it depends on you.
Best regards,
Xudong Peng
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 12, 2021 9:54 AM
All replies
-
User753101303 posted
Hi,
Sounds like asking to write the whole code for you. Try to be explicit about the exact point on which you have a problem so that we can spend time only on what really matters.
I would do that test on the underlying data source rather (you are using EF or ADO.NET ?) than on gridview rows (seems you could show what is wrong right away).
To check the mail address you could use the MailAddress class. Either you can create a MailAddress object from the string or you have an exception which means the mail address is not valid.Friday, April 9, 2021 9:41 AM -
User66371569 posted
thats my problem and if i know wolhe thing why i wolud ask.
thank u
Friday, April 9, 2021 11:22 PM -
User66371569 posted
thats my problem and if i know wolhe thing why i wolud ask.
thank u
Friday, April 9, 2021 11:22 PM -
User-939850651 posted
Hi thepast,
You only need to traverse each row of the GridView in the button event, and return the corresponding result when the situation you describe occurs.
A simple example:
protected void Page_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); if (!Page.IsPostBack) { dt.Columns.AddRange(new DataColumn[] { new DataColumn("id"), new DataColumn("name"), new DataColumn("email"), new DataColumn("vdate") }); dt.Rows.Add(1, "mm", "ms@gmail.com", "1/1/2019"); dt.Rows.Add(2, "aa", "aaaa-gmail.com", string.Empty); dt.Rows.Add(3, "dd", string.Empty, "7/9/2022"); Gv1.DataSource = dt; Gv1.DataBind(); } } protected void checkData_Click(object sender, EventArgs e) { foreach (GridViewRow row in Gv1.Rows) { if (row.RowType == DataControlRowType.DataRow) { string name = row.Cells[1].Text.ToString().Replace(" ",""); string email = row.Cells[2].Text.ToString().Replace(" ", ""); string vdate = row.Cells[3].Text.ToString().Replace(" ", ""); //check if email and vdate empty or valid bool isEmailEmpty = string.IsNullOrEmpty(email); bool isVdateEmpty = string.IsNullOrEmpty(vdate); bool isEmailValid = IsValidEmail(email); DateTime date; if (!isVdateEmpty) { date = DateTime.ParseExact(vdate, "d/M/yyyy", CultureInfo.InvariantCulture); } else { date = DateTime.MinValue; } bool isVdateVaild = (!isVdateEmpty) && (DateTime.Compare(date, DateTime.Now)> 0); if (isEmailValid && !isVdateVaild) {ResultLabel.Text = "vdate is expired for " + name;break; } if (!isEmailValid && isVdateEmpty) {ResultLabel.Text = "email not valid and vdate is empty, fix email and vdate for " + name;break; } if (isEmailEmpty && isVdateVaild) {ResultLabel.Text = "email is empty, please fill email for " + name;break; } } } } static bool IsValidEmail(string email) { try { var addr = new MailAddress(email); return true; } catch { return false; } } protected void updateTB_Click(object sender, EventArgs e) { ResultLabel.Text = ""; DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn("id"), new DataColumn("name"), new DataColumn("email"), new DataColumn("vdate") }); dt.Rows.Add(1, "mm", "ms@gmail.com", "1/1/2023"); dt.Rows.Add(2, "aa", "aaaa-gmail.com", string.Empty); dt.Rows.Add(3, "dd", string.Empty, "7/9/2022"); Gv1.DataSource = dt; Gv1.DataBind(); }
<form id="form1" runat="server"> <div> <asp:GridView runat="server" ID="Gv1" AutoGenerateColumns="true"></asp:GridView> </div> <br /> Result: <asp:Label Text="" ID="ResultLabel" runat="server" /> <br /> <asp:Button Text="Check" ID="checkData" OnClick="checkData_Click" runat="server" /> <asp:Button Text="update" ID="updateTB" OnClick="updateTB_Click" runat="server" /> </form>
Result:
If you need to judge whether the data is empty or valid at the same time, there will be many different results, I think it is necessary to simplify this step. Of course, it depends on you.
Best regards,
Xudong Peng
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 12, 2021 9:54 AM