28 September 2011

ASP:RegularExpressionValidator force validation with JavaScript

Hi All,

You often have the situation when you want to force the validation of the ASP:RegularExpressionValidator using JavaScript.
This is usually because for some reason the Validator is not actioned automatically like: the submit button was initially disabled, or the value of the input to validate is changed through JavaScript.
So you can force the validation of the ASP:RegularExpressionValidator using the Page_ClientValidate JavaScript function.


Code:
<asp:TextBox ID="test" runat="server" 
onblur="if(!Page_ClientValidate()) 
this.focus();"/>

Calling that function will cause the validation of all Validators on the page, so if you want to validate only a Validator which corresponds to a specific ValidationGroup then you would call that JavaScript function like this:

Code:
...
if(Page_ClientValidate(“Required”))
...
Good luck,
Roman

2 comments:

Saqlain Hemani said...

Thank you, this solved my issue!!!

Roman Gherman said...

I'm glad to see that my articles are helping people. ;)

Post a Comment

your thoughts are welcome:

Post a Comment

Need more? Leave comments and subscribe to my blog.

.