0

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 ?

bjan
  • 2,000
  • 7
  • 32
  • 64
  • @CrazyCoderz Are you asking about code for date conversion? – bjan Apr 06 '12 at 13:26
  • Please include some code that you have used so that we can look at. If you give something to look at then we can be in a better situation to give you the correct answer. The more detailed the code that you supply the better.. – Brendan Vogt Apr 06 '12 at 13:45
  • `how can i get the formatted value in Remote function` to achieve this i implemented [this](http://stackoverflow.com/questions/9104196/where-to-put-validator-setdefaults-onkeyup-false-to-disable-mvc3-onkeyup) solution but now my JS to format the date does not get called at all – bjan Apr 09 '12 at 05:24

1 Answers1

0

Since i didn't get any answer so the answer, so far, is "No".

bjan
  • 2,000
  • 7
  • 32
  • 64