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

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

0
heyday2004
Asked:
heyday2004
  • 3
  • 2
  • 2
  • +1
2 Solutions
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now