Skip to content

Instantly share code, notes, and snippets.

@KatsuYuzu
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KatsuYuzu/d1af534aed1e64e22247 to your computer and use it in GitHub Desktop.
Save KatsuYuzu/d1af534aed1e64e22247 to your computer and use it in GitHub Desktop.
jQuery.Validation.Unobtrusive handle sample - http://jsfiddle.net/bsq6e1b2/
.input-validation-error {
background-color: #ff0000;
}
<form>
<div>
<input name="hoge" type="text" data-val-required="The field is required." data-val="true" />
<span data-valmsg-for="hoge" data-valmsg-replace="true"></span>
</div>
<input type="submit" value="save" />
</form>
<ul id="console"></ul>
$.validator.unobtrusive.options = {
errorPlacement: function ($error, $element) {
if ($error.text().length === 0) {
$element.triggerHandler("onSuccess");
} else {
$element.triggerHandler("onError");
}
}
};
$("input").on("onSuccess", function () {
$("#console").prepend("<li>onSuccess</li>");
});
$("input").on("onError", function () {
$("#console").prepend("<li>onError</li>");
});
$(function () {
$("form").valid();
});
name: jQuery.Validation.Unobtrusive handle sample
authors:
- KatsuYuzu
resources:
- //ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/jquery.validate.min.js
- //ajax.aspnetcdn.com/ajax/mvc/5.2.2/jquery.validate.unobtrusive.min.js
wrap: b
@KatsuYuzu
Copy link
Author

jsfiddle と gist の連携。

ただし、外部リソースの読み込み順を守ってくれない不具合があるみたいで、manifestに書かれた順になるまでリロードが必要。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment