Here is an alternative using a CustomValidator, when really needing case-insensitivity server-side and client-side; and the the Upper/lower [A-Za-z] char approach is too much.
This blends the various other answers, using the server-side RegEx object and client-side JavaScript syntax.
CustomValidator:
<asp:CustomValidator ID="cvWeight" runat="server" ControlToValidate="txtWeight"
OnServerValidate="cvWeight_Validate" ClientValidationFunction="cvWeight_Validate"
ValidateEmptyText="true" Text="*" ErrorMessage="Invalid entry." />
Code behind:
protected void cvWeight_Validate(object sender, ServerValidateEventArgs args)
{
Regex re = new Regex(@"^[0-9]*\s*(lbs|kg|kgs)$", RegexOptions.IgnoreCase);
args.IsValid = re.IsMatch(args.Value);
}
Client-side validation function:
function cvWeight_Validate(sender, args) {
var reWeight = /^[0-9]*\s*(lbs|kg|kgs)$/i;
args.IsValid = reWeight.test(args);
}
This is working fine for me, except when using a ValidationSummary. On the client-side validation, the error *
shows, but I can't get the error message to display in the summary. The summary only displays when submitted. I think it's supposed to display; I have a mix of update panels and legacy code, which may be problems.