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)?

Posted on 2004-11-05
Last Modified: 2011-09-20
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,

Question by:heyday2004
    LVL 13

    Assisted Solution

    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();">
    LVL 25

    Accepted Solution

    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;

    LVL 26

    Assisted Solution

    >>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

    Author Comment

    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.

    LVL 25

    Expert Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now