asp.netajaxmaskededitextendermaskededitvalidator

Masked Edit Validator Phone


I am having trouble with the asp.net MaskedEditExtender and MaskedEditValidator. I have created a custom control with a phone mask and a masked validator. There are two issues that I am having that I could use some help to fix.

Issue 1 - I have a ValidationExpression that validates a phone number, but if I set the EnableClientScript = false it will not display the message on the screen. It will validate it correctly and not let them pass, but the message does not display. If it is true it will display, however I only want to do server side validation.

Issue 2 - Sometimes my control is not required. Is there a way to have the validation expression only trigger if the mask is not empty? Right now if it is required the EmptyValueMessage is displayed and the validation is triggered if text is entered. However, if the IsValidEmpty is true the validation expression triggers when it is empty and thus does not allow empties. Is there a way to only trigger it if not empty or do I have to update the expression to allow the empty mask? If I have to update the expression, does anyone have a good example? (sorry, regex is a weakness of mine)

Below is my code.

_maskedEditExtender.ID = "maskedExt" + this.ID;
_maskedEditExtender.ClientIDMode = System.Web.UI.ClientIDMode.Static;
_maskedEditExtender.TargetControlID = this.ID;
_maskedEditExtender.MaskType = MaskedEditType.None;
_maskedEditExtender.Mask = "(999) 999-9999";
_maskedEditExtender.InputDirection = MaskedEditInputDirection.LeftToRight;
_maskedEditExtender.ClearMaskOnLostFocus = false;

_maskedEditValidator.ID = "maskedVal" + this.ID;
_maskedEditValidator.ControlToValidate = this.ID;
_maskedEditValidator.ControlExtender = _maskedEditExtender.ID;
_maskedEditValidator.InitialValue = "(___) ___-____";
_maskedEditValidator.IsValidEmpty = !Required;
_maskedEditValidator.ValidationExpression = @"^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$";
_maskedEditValidator.EmptyValueMessage = "Please enter a phone number.";
_maskedEditValidator.InvalidValueMessage = "Please enter a valid phone number.";

_maskedEditValidator.ValidationGroup = "FormsValidationGroup";
_maskedEditValidator.CssClass = "FormValidation";
_maskedEditValidator.EnableClientScript = false;

Solution

  • Issue 1... I never got a response on how to correct this, and the users preferred it to post back everytime so I just set enableclientscript = false.

    Issue 2... maskedEditValidator.ValidationExpression = @"^[01]?[- .]?(([2-9]\d{2})|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}|(() __-_)$";