ASP.NET MVC 3 client-side validation with parameters

You could use the ValidationParameters property to add custom parameters to the rule: public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { var rule = new ModelClientValidationRule { ErrorMessage = this.ErrorMessage, ValidationType = “futuredate”, }; rule.ValidationParameters.Add(“param1”, “value1”); rule.ValidationParameters.Add(“param2”, “value2”); yield return rule; } which could be used in the adapter: jQuery.validator.unobtrusive.adapters.add( ‘futuredate’, [ ‘param1’, ‘param2’ ], function … Read more

Pass request headers in a jQuery AJAX GET call

As of jQuery 1.5, there is a headers hash you can pass in as follows: $.ajax({ url: “/test”, headers: {“X-Test-Header”: “test-value”} }); From http://api.jquery.com/jQuery.ajax: headers (added 1.5): A map of additional header key/value pairs to send along with the request. This setting is set before the beforeSend function is called; therefore, any values in the … Read more

Image resizing client-side with JavaScript before upload to the server

Here’s a gist which does this: https://gist.github.com/dcollien/312bce1270a5f511bf4a (an es6 version, and a .js version which can be included in a script tag) You can use it as follows: <input type=”file” id=”select”> <img id=”preview”> <script> document.getElementById(‘select’).onchange = function(evt) { ImageTools.resize(this.files[0], { width: 320, // maximum width height: 240 // maximum height }, function(blob, didItResize) { // … Read more

Sending emails with Javascript

The way I’m doing it now is basically like this: The HTML: <textarea id=”myText”> Lorem ipsum… </textarea> <button onclick=”sendMail(); return false”>Send</button> The Javascript: function sendMail() { var link = “mailto:[email protected]” + “[email protected]” + “&subject=” + encodeURIComponent(“This is my subject”) + “&body=” + encodeURIComponent(document.getElementById(‘myText’).value) ; window.location.href = link; } This, surprisingly, works rather well. The only … Read more

How to export JavaScript array info to csv (on client side)?

You can do this in native JavaScript. You’ll have to parse your data into correct CSV format as so (assuming you are using an array of arrays for your data as you have described in the question): const rows = [ [“name1”, “city1”, “some other info”], [“name2”, “city2”, “more info”] ]; let csvContent = “data:text/csv;charset=utf-8,”; … Read more

How to create a file in memory for user to download, but not through server?

Simple solution for HTML5 ready browsers… function download(filename, text) { var element = document.createElement(‘a’); element.setAttribute(‘href’, ‘data:text/plain;charset=utf-8,’ + encodeURIComponent(text)); element.setAttribute(‘download’, filename); element.style.display = ‘none’; document.body.appendChild(element); element.click(); document.body.removeChild(element); } form * { display: block; margin: 10px; } <form onsubmit=”download(this[‘name’].value, this[‘text’].value)”> <input type=”text” name=”name” value=”test.txt”> <textarea name=”text”></textarea> <input type=”submit” value=”Download”> </form> Usage download(‘test.txt’, ‘Hello world!’);