How to clear the text field in client side when the user doesn't pass the regular expression server control validation?

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>

The validation is:
<asp:regularexpressionvalidator id="Regularexpressionvalidator2" style="Z-INDEX: 103; LEFT: 424px; POSITION: absolute; TOP: 88px" Width="393px" Runat="Server" Height="24px" Font-Size="X-Small" Text="Invalid ID number." ValidationExpression="\d{5}" Display="Dynamic" ControlToValidate="TextBox1"> Wrong Zip Code! asp:regularexpressionvalidator></P>

Can add a onClick action to run the ClearWindow() Java script in above code? Since it is running at server, i think there is still problem.

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

-Scott

heyday2004Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

boulder_bumCommented:
Something like this should work (untested):

On client:

<script language="javascript">
   <!--
   function ClientValidate(source, arguments)
   {
      if (/*regex validation succeeds*/)
      {
         arguments.IsValid=true;
      }
      else
      {
         source.value = "";
         arguments.IsValid=false;
       }
   }
   // -->
</script>


Basically you'd use a CustomValidator, set the ClientValidationFunction to "ClientValidate" or whatever you decide to name the script, and do a little extra legwork with RegEx (manually check it) validation on the server-side validation function.

For info on JavaScript RegEx go here:
http://www.regular-expressions.info/javascript.html

For .NET:
http://aspnet.4guysfromrolla.com/articles/022603-1.aspx

0
nauman_ahmedCommented:
ob button_click event:

Page.Validate();
if (!Page.IsValid)
{
   MyTextBox1.Text = "";
}
else
{
    //More code
}

Best, Nauman.
0
boulder_bumCommented:
But what if validation fails on the client and never makes it to the server? D'oh!

Actually Nauman's solution will work if you turn client-side validation off for the page, but you'll take a performance hit.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

nauman_ahmedCommented:
The problem is that you never know if client side validation work all the time; user can turnoff the JavaScript and unless you handle the validation on the server side, they will unable to know whey they cant submit their form. Its always better to validate the form on the server side too.

Best, Nauman.
0
boulder_bumCommented:
I definately agree, I was just saying the TextBox in question won't get cleared like he wants if validation fails on the client.
0
ryerrasCommented:
Ok you both are correct. Nauman, I agree its always a good practice to use server side validation along with client side validation. But that is off topic here

HeyDey,
   As Boulder has suggested you should use customValidator along with RegularExpressionValidator. I tried to simulate your problem, and I came with the following solution.

To your existing page, add a customValidator, and set its clientValidationFunction to "sampleFunc"

the following is the code for javascript function "sampleFunc"
function xx(sender, args)
{
    if(document.getElementById("RegularExpressionValidator1").style.visibility == "visible")
      document.getElementById(document.getElementById("RegularExpressionValidator1").controltovalidate).value ="";
      
    return;
}

Let me explain you each line. The first line
if(document.getElementById("RegularExpressionValidator1").style.visibility == "visible")
checks to see if the RegularExpressionValidator is evaluated the control to invalid. Remember these validators show the error messages only when the control they are validating against fails the validation. Otherwise these controls are invisible. So, when the textbox fails the regularExpression validation, the error message show up in the RegularExpressionValidation control, so at that time its visbility set to "visible"

now the second line
document.getElementById(document.getElementById("RegularExpressionValidator1").controltovalidate).value ="";
gets the control name which the regularExpressionValidator is validating against and set its value to empty

Thats it

Oh ya by the way, this CustomValidator should be added after the RegularExpressionValidator is added to the page. Remember, asp.net generated some javascript to do the client side validation with these validation controls. So, it parses each validation control's tag in the order they are added. So, if you add RegularExpressionValidator before the CustomValidator control, then RegularExpressionValidation Control is validated before the customValidation control.

In your case, since you allready added the regularExpressionValidator, make sure you add the customValidator after this control in the html portion of your page

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
heyday2004Author Commented:
Thanks a lot! Great answers. I had exactly the same question with boulder_bum when nauman presented the solution: after validation fails, it can never make it to the server. But after i closed the client validation as bolder_bum suggested (add EnableClientScript=False in server validation), it works!  For my case, it's enough because I don't need too much validation in client side because i have only one input field. Thanks a lot again for you two guys' excellent answers.
BTW: for boulder_Bum's answer:
 function ClientValidate(source, arguments)
   {
      if (/*regex validation succeeds*/)
      {
         arguments.IsValid=true;


Do you mean I can't use regular expressession server validation (included in asp.net) inside the clientValidate? I really can't figure out how to do it in your if (/*regular validation succeeds */) block, so we have to do it manually, right? Thanks.
0
ryerrasCommented:
Hi Heydey,
 did you take a look at my comment
0
heyday2004Author Commented:
yes and thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.