Is it possible to call the MVC clientside validation with javascript? The scenario is this. I have a textbox and a dropdown. The textbox triggers an AJAX call to populate the dropdown. I was wondering if there is anyway to suppress the AJAX call if the
textbox is not valid. I am using dataannotations for client and server side validation.
Any advice / help would be appreciated. If more information is needed please let me know.
I am using ASP.Net MVC 2. I know that I could use jQuery in this situation but the idea was to try and somehow "tap" into the client validation that MVC does. By using jQuery we would basically be writing validation twice.
Thanks for everyones help so far. I'll let you know if I find out anything from some of the resources that you've pointed me towards.
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Marked as answer by ricka6 on Dec 02, 2010 11:47 PM
You can use jQuery to evaluate the value of textbox. For example if your textbox is named as "txtEmail" then you can get its value like this:
var value = $("#txtEmail").val()
Now, are you doing AJAX post on textbox change event or button (click)?
Either way, you can check the value of the textbox using jQuery and cancel the AJAX post.
I started studying the sources of the client validation linrary in these days for other reasons. There is a "validate"(this is its name) method of the fieldcontext class that do the validation. You can do some tests to see what happens calling it(find it
in the sources and see how it can be callled). However I fear that changes in the way client library is implemented may cause your program...stop working, because I don't know it it is intended for public use.
mlindsay
0 Points
1 Post
MVC Client Side Validation
Dec 02, 2010 03:09 PM|LINK
Is it possible to call the MVC clientside validation with javascript? The scenario is this. I have a textbox and a dropdown. The textbox triggers an AJAX call to populate the dropdown. I was wondering if there is anyway to suppress the AJAX call if the textbox is not valid. I am using dataannotations for client and server side validation.
Any advice / help would be appreciated. If more information is needed please let me know.
I am using ASP.Net MVC 2. I know that I could use jQuery in this situation but the idea was to try and somehow "tap" into the client validation that MVC does. By using jQuery we would basically be writing validation twice.
Thanks for everyones help so far. I'll let you know if I find out anything from some of the resources that you've pointed me towards.
Ajax and MVC
johannes.hie...
Member
441 Points
162 Posts
Re: MVC Client Side Validation
Dec 02, 2010 03:40 PM|LINK
Hi,
I think not as build in functionality. But you could handle this very easily with jquery. Which version of asp.net mvc are u using?
Best Regards,
Johannes
imran_ku07
All-Star
45864 Points
7713 Posts
MVP
Re: MVC Client Side Validation
Dec 02, 2010 04:12 PM|LINK
See,
http://weblogs.asp.net/imranbaloch/archive/2010/07/11/asp-net-mvc-client-side-validation-with-dynamic-contents.aspx
http://weblogs.asp.net/imranbaloch/archive/2010/07/18/asp-net-mvc-client-side-validation-with-ajax-beginform.aspx
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
sachingusain
Star
8786 Points
1702 Posts
Re: MVC Client Side Validation
Dec 02, 2010 04:20 PM|LINK
You can use jQuery to evaluate the value of textbox. For example if your textbox is named as "txtEmail" then you can get its value like this:
Thanks.
francesco ab...
All-Star
20944 Points
3286 Posts
Re: MVC Client Side Validation
Dec 02, 2010 05:39 PM|LINK
I started studying the sources of the client validation linrary in these days for other reasons. There is a "validate"(this is its name) method of the fieldcontext class that do the validation. You can do some tests to see what happens calling it(find it in the sources and see how it can be callled). However I fear that changes in the way client library is implemented may cause your program...stop working, because I don't know it it is intended for public use.
Mvc Controls Toolkit | Data Moving Plug-in Videos
ryanw51
Contributor
2363 Points
511 Posts
Re: MVC Client Side Validation
Dec 02, 2010 07:08 PM|LINK
If you're using MVC 3.0 unobtrusive validation with jquery validation, you can turn it off quite easily.
$('form').validate({ ignore: 'form *' });If you're using the microsoft client validation... eek.