private void OnKeyPress(object sender, KeyPressEventArgs e) { _MaskedTextBox sd = (_MaskedTextBox)sender; switch (m_mask) { case Mask.DateOnly: sd.MaskDate(e); break; case Mask.PhoneWithArea: sd.MaskPhoneSSN(e, 3, 3); break; case Mask.IpAddress: sd.MaskIpAddr(e); break; case Mask.SSN: sd.MaskPhoneSSN(e, 3, 2); break; case Mask.Decimal: sd.MaskDecimal(e); break; case Mask.Digit: sd.MaskDigit(e); break; } }
private void OnLeave(object sender, EventArgs e) { _MaskedTextBox sd = (_MaskedTextBox)sender; Regex regStr; switch (m_mask) { case Mask.DateOnly: regStr = new Regex(@"\d{2}/\d{2}/\d{4}"); if (!regStr.IsMatch(sd.Text)) { errorProvider1.SetError(this, "Date is not valid; mm/dd/yyyy"); } break; case Mask.PhoneWithArea: regStr = new Regex(@"\d{3}-\d{3}-\d{4}"); if (!regStr.IsMatch(sd.Text)) { errorProvider1.SetError(this, "Phone number is not valid; xxx-xxx-xxxx"); } break; case Mask.IpAddress: short cnt = 0; int len = sd.Text.Length; for (short i = 0; i < len; i++) { if (sd.Text[i] == '.') { cnt++; if (i + 1 < len) { if (sd.Text[i + 1] == '.') { errorProvider1.SetError(this, "IP Address is not valid; x??.x??.x??.x??"); break; } } } } if (cnt < 3 || sd.Text[len - 1] == '.') { errorProvider1.SetError(this, "IP Address is not valid; x??.x??.x??.x??"); } break; case Mask.SSN: regStr = new Regex(@"\d{3}-\d{2}-\d{4}"); if (!regStr.IsMatch(sd.Text)) { errorProvider1.SetError(this, "SSN is not valid; xxx-xx-xxxx"); } break; case Mask.Decimal: break; case Mask.Digit: break; } }