[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 862
  • Last Modified:

How to clear text field after the validation controls? And how can I make sure the error message will not appear in the page(just in my popup window)?

I wrote a very simple validatoin control to make sure the user provides valid zip code. Here are my problems:
1. When user can't pass the validation, there will be a pop up window, how can i clear the text field after user click the ok and return control to the text box? Because the validation is in client side, so i can't use TextBox1.Text=""; etc.

2. I added the ShowMessageBox to create a pop-up message box with the ValidationSummary control. But there is still a message in the page (same as the one in the pop up window). How can i make sure the error messages only appear in my popup window instead of appearing both in popup window and in the page? I tried to set control's Display property to none, so the in-line error message disappeared. But the summary is still in the page. Below are the code block: (add validation to TextBox1)

<asp:regularexpressionvalidator id="Regularexpressionvalidator2" Width="164px" Runat="Server" Font-Size="X-Small" Height="2px" ValidationExpression="\d{5}" ErrorMessage="Oops, you need to provide a valid FIVE DIGIT NUMBER here! Please try again." Display="none" ControlToValidate="TextBox1"></asp:regularexpressionvalidator></P>
<asp:ValidationSummary Runat="server" ShowMessageBox="True" DisplayMode=SingleParagraph id="ValidationSummary1" />

Thanks a lot,

3 Solutions
in your popup page
you can use javascript

<SCRIPT LANGUAGE=javascript>
function CloseWindow()
  window.opener.myFormName.myField.value = "";

 <input type="button" name="Button" value="Close" onClick="CloseWindow();">
Usually I do it in the following way:

<asp:requiredfieldvalidator id="rfvTxtName" runat="server" ControlToValidate="TxtName" Display="None"

<asp:validationsummary id="VSUserForm" runat="server" Font-Names="Verdana" Width="440px" Font-Size="XX-Small"
                                          ShowSummary="False" ShowMessageBox="True" HeaderText="Please provide information for the following required field(s): <br />--------------------------------------------------------------------"

This will display the javascript popup containing any validation error. Also its a good idea to re-validate the page when the user select submit as javascript being disabled will not show anything at all in the above code:

private void btnSubmit_Click(object Sender, EventArgs e)
  if (!Page.IsValid)
     this.VSUserForm.ShowSummary = true;

>>I wrote a very simple validatoin control to make sure the user provides valid zip code.
1. add another customValidator control and set the property
ClientValidationFunction = myvalidation
.. within this client-side function .. do ur validation and then set the control as blank using
<script language=javascript>
function myvalidation()
//use regex to validate ur control.. if validation fails then
document.getElementById("urcontrol").value = '';
2. set the "ShowSummary" to "False" for the ValidationSummary control
heyday2004Author Commented:
Sorry for the late reply. Thanks a lot for all the excellent answers! The second problem is solved. But the first problem is still there: How to clear the text field in client side when the user doesn't pass the regular expression validation? My button is declared as:
<asp:button id="btnEnter"  style="Z-INDEX: 101; LEFT: 720px; POSITION: absolute; TOP: 32px" runat="server"
Height="36px" Font-Bold="True" Font-Size="Small" Text="ENTER"></asp:button>

So I don't think I can add a onClick action to run the ClearWindow() Java script in above code because it is running at server?

So Rejojohny suggested put a customvalidation to combine the regular expression and clear text field. But how? I couldn't figure out the details because it was said the custom control is used in addition to the validation that that the server control can't do. So usually just add a custom validation after the regular server validation. But now, what I need is some way to trigger the clear text field action (java script) when the server validation failed. Please help. Thanks a lot again for the great answers.

You can handle that in the ValidationControl_ServerValidate event.
HTH, Nauman.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now