Link to home
Start Free TrialLog in
Avatar of heyday2004
heyday2004

asked on

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

Avatar of boulder_bum
boulder_bum

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

ob button_click event:

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

Best, Nauman.
SOLUTION
Avatar of boulder_bum
boulder_bum

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
I definately agree, I was just saying the TextBox in question won't get cleared like he wants if validation fails on the client.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of heyday2004

ASKER

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.
Hi Heydey,
 did you take a look at my comment
yes and thanks!