I am trying to implement what we can see here:
http://weblogs.asp.net/cibrax/archive/2008/08/01/combining-jquery-validation-with-asp-net-mvc.aspx
The problem I am having is that it does not use the remote validators.
<form action="<%=Url.Action("Register", "Home")%>" method="post" id="form-sign-up">
In my case, everytime I hit submit, it posts into the Register action - I never seem to hit the Json validator methods. In fact I download the sample and the remote posts don't work for me. Does the remote part work in the sample for anyone else?
Does anyone know how to get them to work or have a better example ?
Basically what I have is a modal pop up edit form. I want to do all the validation before posting to the save (or in this case Register
) method.
I just have to check it is required and the remote validator is to che开发者_JS百科ck if it is duplicate. In my example I miss the validators and end up in the save method. Obviously I only want to save if the field is filled in and isn't a duplicate.
EDIT 0:
I'm using MVC 2
<form action="/Region/Save" method="post" id="Form-AddRegion">
<div class="editor-label">
<%: Html.Label("Region:") %>
<%: Html.TextBoxFor(model => model.Region, new { @class = "edit required remote"}) %>
</div>
<p>
<input type="submit" value="Save" />
<input type="button" onClick="window.parent.CloseWindow();" value="Cancel" />
</p>
</form>
public ActionResult ValidateRegion(string name)
{
if (!something)
return Json(true, JsonRequestBehavior.AllowGet);
return Json(false, JsonRequestBehavior.AllowGet);
}
<script type="text/javascript">
$(document).ready(function () {
alert('jQuery is referenced properly');
$("#Form-AddRegion").validate({
rules:
{
//rules each for specified input-id
Region:
{
required: true,
remote: '<%=Url.Action("ValidateRegion","Region") %>'
}
},
messages:
{
Region:
{
required: "Please provide a Region",
remote: jQuery.format("{0} is already in use")
}
}
});
});
</script>
So the problem is that on submit, I always post into the save method /Region/Save
before the validator ValidateRegion
! Am I missing a route in the global.asax.cs
?
I need to know when does the javascript code post/get into the validator method? I would assume on submit but it never seems to get called !?
It sure is annoying when people don't read your problem and point you at the main documentation reference, funnily enough, I've already been there.
Any kind of form validation must be connected to onsubmit function if this function will return false form will not be submited .
<form onSubmit="function(){return false;}">
will never be submited
to connect jquery validate to onsubmit is done by validate() function which NEEDS to be done on document ready
and one more thing all inputs NEEDS name attribute to be able to be validated
more documentation and lots of examples can be found here http://docs.jquery.com/Plugins/Validation/
EDIT
This is your code in jsFiddle : http://jsfiddle.net/fzf3q/1/
it works there only sugesstion is make sure that validate function is included just before using it run :
alert($("#Form-AddRegion").validate)
精彩评论