using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using EBIZ.WebControls;
using System.Text.RegularExpressions;
namespace System
{
/// <summary>
/// ValidatorUtility 的摘要描述
/// </summary>
public static class ValidatorUtility
{
/// <summary>
/// 檢查數字類型
/// </summary>
/// <param name="val"></param>
/// <param name="IntLength"></param>
/// <param name="DecimalLength"></param>
/// <returns></returns>
public static bool chkFloat(this string val, int IntLength, int DecimalLength)
{
if (val.IsEmpty())
{
return true;
}
else
{
decimal decvalue;
if (decimal.TryParse(val, out decvalue))
{
string[] arr = decvalue.ToString().Split('.');
if (arr.Length == 1)
{
if (arr[0].Length > IntLength)
{
return false;
}
return true;
}
else if (arr.Length == 2)
{
if (arr[0].Length > 0 && arr[0].Length > IntLength)
{
return false;
}
if (arr[1].Length > 0 && arr[1].Length > DecimalLength)
{
return false;
}
return true;
}
}
return false;
}
}
/// <summary>
/// 檢查是否整型
/// </summary>
/// <param name="val"></param>
/// <returns></returns>
public static bool chkInt(this string val)
{
int value;
return int.TryParse(val,out value);
}
/// <summary>
/// 元件輸入錯誤
/// </summary>
/// <param name="ctl"></param>
public static void IsError(this Control ctl)
{
ctl.Focus();
PropertyInfo info = ctl.GetType().GetProperty("CssClass");
string css = info.GetValue(ctl, null).ToString().Replace("error", "");
info.SetValue(ctl, css + " error", null);
if (ctl is ListControl && !(ctl is DropDownList))
{
ctl.GetType().GetProperty("BorderColor").SetValue(ctl, System.Drawing.Color.Red, null);
ctl.GetType().GetProperty("BorderWidth").SetValue(ctl, Unit.Pixel(1), null);
}
}
/// <summary>
/// 清除元件錯誤樣式
/// </summary>
/// <param name="ctls"></param>
public static void removeErrorStyle(params Control[] ctls)
{
foreach (Control ctl in ctls)
{
PropertyInfo info = ctl.GetType().GetProperty("CssClass");
string css = info.GetValue(ctl, null).ToString().Replace("error", "");
info.SetValue(ctl, css, null);
if (ctl is ListControl && !(ctl is DropDownList))
{
ctl.GetType().GetProperty("BorderWidth").SetValue(ctl, Unit.Pixel(0), null);
}
}
}
/// <summary>
/// 驗證數據
/// </summary>
/// <param name="lblErr"></param>
/// <param name="ctls"></param>
/// <returns></returns>
public static bool Validator(Label lblErr, params Control[] ctls)
{
bool blnOk = true;
removeErrorStyle(ctls);
foreach (Control ctl in ctls)
{
if (ctl.Visible)
{
Type ctlType = ctl.GetType();
string css = ctlType.GetProperty("CssClass").GetValue(ctl, null).ToString().ToLower();
string errMsg = ctlType.GetProperty("ToolTip").GetValue(ctl, null).ToString();
if (ctlType.GetProperty("SelectedItem") != null)
{
if ((css.Contains("chkempty") || css.Contains("chkchecked")) &&
ctlType.GetProperty("SelectedValue").GetValue(ctl, null).ToString() == "")
{
blnOk = false;
ctl.IsError();
if ((ctl is DropDownList || ctl is CheckBoxList) && errMsg != "")
{
lblErr.Text = errMsg;
}
else
{
lblErr.Text = "該欄位必選!";
}
break;
}
}
else if (ctlType.GetProperty("Text") != null)
{
string value = ctlType.GetProperty("Text").GetValue(ctl, null).ToString();
if (css.Contains("chkempty") && value.IsEmpty())
{
blnOk = false;
ctl.IsError();
lblErr.Text = errMsg.IsEmpty() ? "該欄位必填!" : errMsg;
break;
}
if (!value.IsEmpty())
{
if (css.Contains("chklength"))
{
int len = Convert.ToInt32(((TextBox)ctl).Attributes["maxLen"]);
if (value.Trim().Length > len)
{
blnOk = false;
ctl.IsError();
lblErr.Text = "該欄位最多只接受 " + len + " 個字元數";
if (ctlType.GetProperty("TextMode").GetValue(ctl, null).ToString() == "MultiLine")
{
lblErr.Text += "(換行佔2個字元)!";
}
break;
}
}
if (css.Contains("chkint") && !value.chkInt())
{
blnOk = false;
lblErr.Text = "該欄位只接受整數!";
ctl.IsError();
break;
}
if (css.Contains("chkfloat"))
{
int intLen = Convert.ToInt32(((TextBox)ctl).Attributes["IntLength"]);
int deciLen = Convert.ToInt32(((TextBox)ctl).Attributes["DecimalLength"]);
if (!value.chkFloat(intLen, deciLen))
{
blnOk = false;
lblErr.Text = String.Format("該欄位只接受 {0} 位正整數 及小數點後 {1} 位!", intLen, deciLen);
ctl.IsError();
break;
}
}
if (css.Contains("chkThanZero"))
{
if (Convert.ToDouble(value) <= 0)
{
blnOk = false;
lblErr.Text ="該欄位必須大於0!";
ctl.IsError();
break;
}
}
}
}
}
}
return blnOk;
}
}
}