Skip to content

Instantly share code, notes, and snippets.

@ptsurbeleu
Last active October 21, 2015 01:21
Show Gist options
  • Save ptsurbeleu/67f7b5158f715a29a48d to your computer and use it in GitHub Desktop.
Save ptsurbeleu/67f7b5158f715a29a48d to your computer and use it in GitHub Desktop.
Async validation in UI extension (Windows Azure Pack)
<!-- SYNOPSIS: Sample HTML snippet from an imaginary UI extension that makes use of async validation -->
<!-- NOTE: This UI snippet will send an ajax request to ChangePassword/ValidatePasswordStrength uri to validate the users' input -->
<div>
<div id="changeMyPasswordForm">
<div class="drawer-form-item">
<label for="newPassword">New Password</label>
<div>
<input id="newPassword" name="newPassword" type="text" data-val="true"
data-val-length-min="6" data-val-length-max="64"
data-val-remote="enabled"
data-val-remote-url="ChangePassword/ValidatePasswordStrength" />
</div>
</div>
</div>
</div>
// SYNOPSIS: This is a sample controller that pretends to change/validate user's password complexity
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.ServiceModel.Web;
using System.Web.Mvc;
using System.Web.UI;
namespace MyAzurePackExtension {
[OutputCache(Location = OutputCacheLocation.None)]
[JsonExceptionHandler]
public class ChangePasswordController : Controller {
/// <summary>
/// POST: /ChangePassword/ValidatePasswordStrength
/// </summary>
[HttpPost]
[ActionName("ValidatePasswordStrength")]
[OutputCache(Location = OutputCacheLocation.None)]
[AspNetCacheProfile("NoCache")]
public JsonResult ValidatePasswordStrengthAsync(string newPassword)
{
// Just a very primitive validation to demonstrate the error case
if (newPassword.StartsWith("password", StringComparison.OrdinalIgnoreCase)) {
return Json("New password does not meet the complexity requirements, please try again.");
}
// Here we report back to the UI, pwd string has passed the validation criteria - show green mark to the user
return Json(true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment