c# - Validating School Year -



c# - Validating School Year -

im asking schoolyear.

just example

2013-2014 (startyear-endyear)

how validate if user come in school year (ex. 2013-2014)

this have tried far.

private void textbox_validating(object sender, canceleventargs e) { if (!string.isnullorwhitespace(studentlastschoolattendedschoolyeartextbox.text)) { int fromyear = 0; int toyear = 0; string[] years = regex.split(studentlastschoolattendedschoolyeartextbox.text, @"-"); fromyear = int.parse(years.firstordefault().tostring()); if (fromyear.tostring().length == 4) { if (years.count() > 1) { if (!string.isnullorwhitespace(years.lastordefault())) { toyear = int.parse(years.lastordefault().tostring()); if (fromyear >= toyear) { e.cancel = true; messagebox.show("the 'from year' must lesser 'to year'.\n\njust leave empty avoid validating.", text, messageboxbuttons.ok, messageboxicon.exclamation); } } else { e.cancel = true; messagebox.show("please come in valid school range year format.\nex.: 2010-2011\n\njust leave empty avoid validating.", text, messageboxbuttons.ok, messageboxicon.exclamation); } } else { studentlastschoolattendedschoolyeartextbox.text = fromyear.tostring() + "-" + convert.toint32(fromyear + 1).tostring(); } } else { e.cancel = true; messagebox.show("please come in valid school range year format.\nex.: 2010-2011\n\njust leave empty avoid validating.", text, messageboxbuttons.ok, messageboxicon.exclamation); } } }

assuming 2013-2014 valid format, might function works:

public static bool isschoolyearformat(string format, int minyear, int maxyear) { string[] parts = format.trim().split(new[] { '-' }, stringsplitoptions.removeemptyentries); if (parts.length == 2) { int fromyear; int toyear; if (int.tryparse(parts[0], out fromyear) && int.tryparse(parts[1], out toyear)) { if (fromyear >= minyear && toyear <= maxyear && fromyear + 1 == toyear) homecoming true; } } homecoming false; }

c# date

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -