Is there any possibility of rectifying the input at client side using Remote Validation? For example, the user will enter the date as 010101 which means 01-Jan-2001, can Remote Validation reflect/pass the rectified value (010101 into 01-Jan-2001) at/to client side?
I have a scenario where i have JS to format the input into correct date format. Later on i had to use RemoteValidation. My remote validation receives date in ddmmyy format (RemoteValidation gets called before JS), it first converts it into correct date and then perform validation, and then my JS does not get called at all and so 010101 is not get converted into 01-Jan-2001.
Edit
There are two things i would like to get help for
1- Is there anyway a remote validation function can modify the model/data passed to it for validation and then pass it back to the view so that user can see the modified version of the model/data
[Deleted:2- I have JavaScript for a date field which formats the date when focus is lost. It is working fine. When i used Remote validation along with JS, the script does not get called at all.]
Edit
Model
public class master
{
public string sometext { get; set; }
public child mychild { get; set; }
}
public class child
{
public child()
{
thedate = DateTime.MaxValue;
}
[Remote("ValidateDate", "Test", ErrorMessage = "Invalid Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:mm/dd/yyyy}", NullDisplayText = "Enter Date")]
public DateTime? thedate { get; set; }
}
View
@model Models.master
@using Web.Framework
@{
Layout = null;
}
<html>
<head>
<title>Test - My ASP.NET MVC Application</title>
<script src="@Url.Scripts("jquery-1.7.1.min.js")" type="text/javascript"></script>
<script src="@Url.Scripts("jquery-ui-1.8.11.js")" type="text/javascript"></script>
<script src="@Url.Scripts("modernizr-2.0.6-development-only.js")" type="text/javascript"></script>
<script src="@Url.Scripts("AjaxLogin.js")" type="text/javascript"></script>
<script src="@Url.Scripts("jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Scripts("jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Scripts("jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<script src="@Url.Scripts("MicrosoftAjax.js")" type="text/javascript"></script>
<script src="@Url.Scripts("MicrosoftMvcAjax.js")" type="text/javascript"></script>
<script src="@Url.Scripts("myScripts.js")" type="text/javascript"></script>
</head>
<body>
@using (Html.BeginForm("testSubmit", "Test"))
{
@Html.LabelFor(m => m.sometext)
<br />
@Html.TextBoxFor(m => m.sometext)
<br />
@Html.LabelFor(m => m.mychild.thedate)
<br />
@Html.TextBoxFor(m => m.mychild.thedate, new { onblur = "doDate(this, '');" })
@Html.ValidationMessageFor(m => m.mychild.thedate)
<br />
<input type="submit" value = "Submit me" />
}
</body></html>
Controller
public ActionResult testSubmit(master model)
{
@ViewBag.Message = "OK";
return View("response");
}
public JsonResult ValidateDate(DateTime? thedate)
{
return Json(HttpContext.Request.QueryString["mychild.thedate"].ToString(), JsonRequestBehavior.AllowGet);
}
My remote validator always receives null in thedate
, but i can access the value from query string but the value is unformatted i.e. Remote validation gets called before JS which means i have to format the date first in remote function and then validate, once validated then this input is formatted again by JS and both have to be synced.
Now, how can i get the formatted value in Remote function OR pass the formatted value from Remote function to the View ?
Since i didn't get any answer so the answer, so far, is "No".