Setting ValidationExpression clientSide
-
I have web page that I want to switch the validationExpression for control based on a checkBox. I'm trying the code in the script portion of the web page. When I submit the page and evaluate the Validation Control on the Page_Load during Postback the control has the same expression as defined in the .aspx file body, eg <asp:RegularExpressionValidator ID="EmailRegexValidator" runat="server" ControlToValidate="Email" ErrorMessage="Invalid Email" ValidationExpression=".*@.{2,}\..{2,}"> ///// Does not seem to change in postback </asp:RegularExpressionValidator> Any ideas on what I need to do to make this work? <script type="text/javascript" language="javascript"> function InitValidators() { // retrieve instance of our checkbox var checkbox = document.getElementById('<%=enableValidatorsCheckBox.ClientID%>'); // enable/disable all validators on page based on checkbox state ValidatorsEnabled(checkbox.checked); } function ValidatorsEnabled(state) { ValidatorEnable(document.getElementById('<%=NameRequiredFieldValidator.ClientID%>'), state); ValidatorEnable(document.getElementById('<%=EmailRequiredFieldValidator.ClientID%>'), state); ValidatorEnable(document.getElementById('<%=EmailRegexValidator.ClientID%>'), state); if (state) setValidation(".*@.{2,}\..{2,}") else &nbs
-
I have web page that I want to switch the validationExpression for control based on a checkBox. I'm trying the code in the script portion of the web page. When I submit the page and evaluate the Validation Control on the Page_Load during Postback the control has the same expression as defined in the .aspx file body, eg <asp:RegularExpressionValidator ID="EmailRegexValidator" runat="server" ControlToValidate="Email" ErrorMessage="Invalid Email" ValidationExpression=".*@.{2,}\..{2,}"> ///// Does not seem to change in postback </asp:RegularExpressionValidator> Any ideas on what I need to do to make this work? <script type="text/javascript" language="javascript"> function InitValidators() { // retrieve instance of our checkbox var checkbox = document.getElementById('<%=enableValidatorsCheckBox.ClientID%>'); // enable/disable all validators on page based on checkbox state ValidatorsEnabled(checkbox.checked); } function ValidatorsEnabled(state) { ValidatorEnable(document.getElementById('<%=NameRequiredFieldValidator.ClientID%>'), state); ValidatorEnable(document.getElementById('<%=EmailRequiredFieldValidator.ClientID%>'), state); ValidatorEnable(document.getElementById('<%=EmailRegexValidator.ClientID%>'), state); if (state) setValidation(".*@.{2,}\..{2,}") else &nbs
dwolver wrote:
document.getElementById("EmailRegexValidator").validationexpression = someVal;
This won't work. ASP.NET processes validator controls on sever side and render HTML and JS to the response. Validator control may render as a span or div and there is no property called
validationexpression
for DIV or SPAN. Your best bet would be to use a custom validator. And use the regular expression methods provided in JS. :)Navaneeth How to use google | Ask smart questions