1

I've got an asp:RequiredFieldValidator, which checks an asp:TextBox to see if it is blank or not.

On a button press, I would like to make the validator validate the textbox. This must be done through jQuery/javascript, as the button is an html input button.

Any ideas? I've read many resources on the web, but have not managed to accomplish this (i.e. the calling of validation through jquery)

1
  • My answer does what you're looking for. I've provided a full example of how to implement it. Commented Nov 4, 2011 at 17:08

3 Answers 3

2

You can trigger validation from JavaScript like this:

var isValid = Page_ClientValidate("");

If you only want to validate controls in a certain group just pass the group name into the function:

var isValid = Page_ClientValidate("GroupName");

Here's a quick example:

<script type="text/javascript">
    validateStuff = function(){
       return Page_ClientValidate("ValidateTextBox");
    }
</script>  
<asp:TextBox ID="TextBox1" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
    ControlToValidate="TextBox1" 
    ValidationGroup="ValidateTextBox" 
    Display="Dynamic" 
    ErrorMessage="*" ...>
</asp:RequiredFieldValidator>
<input type="button" value="Click Me" onclick="return validateStuff();" />
Sign up to request clarification or add additional context in comments.

5 Comments

I didn't down vote you, but possibly because this side-steps the framework. Just set the ValidationGroup in the Button and create a CustomValidator it will work.
The original question says there is no ASP Button. So there wouldn't be a ValidationGroup property to assign.
The same principle still applies. Just use onclick to call the function instead. This approach doesn't side-step the framework at all. It just manually triggers the validation.
Yea, I was just pointing out why @drdwilcox solution was not complete.
Thanks man. Had to look everywhere only to find this perfect solution sitting over here. cheers!
0

if its jQuery validation your after - there are few better / easier to implement than this one -> http://bassistance.de/jquery-plugins/jquery-plugin-validation/

3 Comments

I'd rather not use a plugin. There's no way to do what I'm after?
My answer tells you how. It's really not that hard.
It may not seem difficult to you, but others are just learning the ropes. I think OP IS just trying to survey the different options.
0

All you need is a CustomValidator. Add OnServerValidate for server-side, and ClientValidationFunction to your javascript. Here is a reasonably good intro to CustomValiditors but no jQuery reference: https://web.archive.org/web/20211020145934/https://www.4guysfromrolla.com/articles/073102-1.aspx

2 Comments

I'll try implementing this! Many thanks for the assistance :)
The CustomValidator is actually not the right solution here. OP simply needs a RequiredFieldValidator to validate on button click. Using a CustomValidator would be overkill for such a simple task.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.