How to recover file contents after Notepad++ crash

Notepad++ is generally a pleasure to use but it does very occasionally crash and empty whatever file you happened to be editing at the time too… Here’s where you can find a backup version to recover your file if this ever happens to you too:

C:\Users\YOUR_USERNAME\AppData\Roaming\Notepad++\backup

Note: At the time of writing I’m running Notepad++ v6.7.8.2 on Windows 7 Professional, but you’ve not got anything to lose by trying this for other versions of Notepad++/Windows.

Thanks to Indrajit on Stack Overflow for posting the solution originally!

Form validation made super easy with jQuery

We spend a lot of time coding both client and server-side form valdilation.

This is the routine I now always use for client-side validation, which has been refined over the years to become what I’d consider to be some pretty tight code, whilst providing good quality feedback to the user to help them complete forms quickly and with minimal confusion.

Hopefully you can learn a trick or two that you can use yourself, by looking at the code…

function errorBefore(error, $insertBefore) {
  $('<p class="error">' + error + '</p>').insertBefore($insertBefore);
}

$(function () {

  $("#my-form").submit(function (e) {

    // remove any errors from previous form submission
    $('.error', $(this)).remove();

    // get jQuery reference to form fields
    var $name = $('#name');
    var $email = $('#email');

    // validate name input
    if (!$name.val()) {
      errorBefore('Please enter your name.', $name);
    }

    // validate email input
    if (!$email.val()) {
      errorBefore('Please enter your email.', $email);
    }

    // check for errors
    if ($('.error', $(this)).length) {

      // find first error and focus on form field it relates to
      $('.error', $(this)).first().next('input, select, textarea').focus();

      // stop form submission
      e.preventDefault();
    }

  });

});

No more undefined index using PHP’s $_GET and $_POST

It’s very annoying having to check whether $_GET['somevalue'] is set before checking its value – but if you don’t, your code is going to throw a a lot of Notice: Undefined index warnings and that’s never good.

I’ve written a quick class to make every PHP developer’s life a bit easier. It’s based on CodeIgniter‘s $this->input class. You can use it as follows and never have to worry about whether the index exists or not… It will just get set to NULL if it doesn’t.

class Input {
     function get($name) {
          return isset($_GET[$name]) ? $_GET[$name] : null;
     }

     function post($name) {
          return isset($_POST[$name]) ? $_POST[$name] : null;
     }

     function get_post($name) {
          return $this->get($name) ? $this->get($name) : $this->post($name);
     }
}
$input = new Input;

$page = $input->get('page'); // // look in $_GET
$page = $input->post('page'); // // look in $_POST
$page = $input->get_post('page'); // look in both $_GET and $_POST

MD5 for sencha touch

There’s no native MD5 functionality in JavaScript, but Joseph Meyers has written his own function, which accoring to this stackoverflow thread is the fastest one in town!

It didn’t take much work, but I’ve converted it into a utility class, ready to drop into your Sencha Touch projects.

Ext.define('MyApp.util.Crypto', {
singleton : true,
alternateClassName : ['Crypto'],

constructor: function(config) {
this.initConfig(config);
},

md5cycle: function(x, k) {
var me = this;
var a = x[0], b = x[1], c = x[2], d = x[3];

a = me.ff(a, b, c, d, k[0], 7, -680876936);
d = me.ff(d, a, b, c, k[1], 12, -389564586);
c = me.ff(c, d, a, b, k[2], 17,  606105819);
b = me.ff(b, c, d, a, k[3], 22, -1044525330);
a = me.ff(a, b, c, d, k[4], 7, -176418897);
d = me.ff(d, a, b, c, k[5], 12,  1200080426);
c = me.ff(c, d, a, b, k[6], 17, -1473231341);
b = me.ff(b, c, d, a, k[7], 22, -45705983);
a = me.ff(a, b, c, d, k[8], 7,  1770035416);
d = me.ff(d, a, b, c, k[9], 12, -1958414417);
c = me.ff(c, d, a, b, k[10], 17, -42063);
b = me.ff(b, c, d, a, k[11], 22, -1990404162);
a = me.ff(a, b, c, d, k[12], 7,  1804603682);
d = me.ff(d, a, b, c, k[13], 12, -40341101);
c = me.ff(c, d, a, b, k[14], 17, -1502002290);
b = me.ff(b, c, d, a, k[15], 22,  1236535329);

a = me.gg(a, b, c, d, k[1], 5, -165796510);
d = me.gg(d, a, b, c, k[6], 9, -1069501632);
c = me.gg(c, d, a, b, k[11], 14,  643717713);
b = me.gg(b, c, d, a, k[0], 20, -373897302);
a = me.gg(a, b, c, d, k[5], 5, -701558691);
d = me.gg(d, a, b, c, k[10], 9,  38016083);
c = me.gg(c, d, a, b, k[15], 14, -660478335);
b = me.gg(b, c, d, a, k[4], 20, -405537848);
a = me.gg(a, b, c, d, k[9], 5,  568446438);
d = me.gg(d, a, b, c, k[14], 9, -1019803690);
c = me.gg(c, d, a, b, k[3], 14, -187363961);
b = me.gg(b, c, d, a, k[8], 20,  1163531501);
a = me.gg(a, b, c, d, k[13], 5, -1444681467);
d = me.gg(d, a, b, c, k[2], 9, -51403784);
c = me.gg(c, d, a, b, k[7], 14,  1735328473);
b = me.gg(b, c, d, a, k[12], 20, -1926607734);

a = me.hh(a, b, c, d, k[5], 4, -378558);
d = me.hh(d, a, b, c, k[8], 11, -2022574463);
c = me.hh(c, d, a, b, k[11], 16,  1839030562);
b = me.hh(b, c, d, a, k[14], 23, -35309556);
a = me.hh(a, b, c, d, k[1], 4, -1530992060);
d = me.hh(d, a, b, c, k[4], 11,  1272893353);
c = me.hh(c, d, a, b, k[7], 16, -155497632);
b = me.hh(b, c, d, a, k[10], 23, -1094730640);
a = me.hh(a, b, c, d, k[13], 4,  681279174);
d = me.hh(d, a, b, c, k[0], 11, -358537222);
c = me.hh(c, d, a, b, k[3], 16, -722521979);
b = me.hh(b, c, d, a, k[6], 23,  76029189);
a = me.hh(a, b, c, d, k[9], 4, -640364487);
d = me.hh(d, a, b, c, k[12], 11, -421815835);
c = me.hh(c, d, a, b, k[15], 16,  530742520);
b = me.hh(b, c, d, a, k[2], 23, -995338651);

a = me.ii(a, b, c, d, k[0], 6, -198630844);
d = me.ii(d, a, b, c, k[7], 10,  1126891415);
c = me.ii(c, d, a, b, k[14], 15, -1416354905);
b = me.ii(b, c, d, a, k[5], 21, -57434055);
a = me.ii(a, b, c, d, k[12], 6,  1700485571);
d = me.ii(d, a, b, c, k[3], 10, -1894986606);
c = me.ii(c, d, a, b, k[10], 15, -1051523);
b = me.ii(b, c, d, a, k[1], 21, -2054922799);
a = me.ii(a, b, c, d, k[8], 6,  1873313359);
d = me.ii(d, a, b, c, k[15], 10, -30611744);
c = me.ii(c, d, a, b, k[6], 15, -1560198380);
b = me.ii(b, c, d, a, k[13], 21,  1309151649);
a = me.ii(a, b, c, d, k[4], 6, -145523070);
d = me.ii(d, a, b, c, k[11], 10, -1120210379);
c = me.ii(c, d, a, b, k[2], 15,  718787259);
b = me.ii(b, c, d, a, k[9], 21, -343485551);

x[0] = me.add32(a, x[0]);
x[1] = me.add32(b, x[1]);
x[2] = me.add32(c, x[2]);
x[3] = me.add32(d, x[3]);

},

cmn: function(q, a, b, x, s, t) {
var me = this;
a = me.add32(me.add32(a, q), me.add32(x, t));
return me.add32((a << s) | (a >>> (32 - s)), b);
},

ff: function (a, b, c, d, x, s, t) {
return this.cmn((b & c) | ((~b) & d), a, b, x, s, t);
},

gg: function(a, b, c, d, x, s, t) {
return this.cmn((b & d) | (c & (~d)), a, b, x, s, t);
},

hh: function(a, b, c, d, x, s, t) {
return this.cmn(b^c^d, a, b, x, s, t);
},

ii: function(a, b, c, d, x, s, t) {
return this.cmn(c^(b | (~d)), a, b, x, s, t);
},

md51: function(s) {
var me = this;
txt = '';
var n = s.length,
state = [1732584193, -271733879, -1732584194, 271733878],
i;
for (i = 64; i <= s.length; i += 64) {
me.md5cycle(state, me.md5blk(s.substring(i - 64, i)));
}
s = s.substring(i - 64);
var tail = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
for (i = 0; i < s.length; i++)
tail[i >> 2] |= s.charCodeAt(i) << ((i % 4) << 3);
tail[i >> 2] |= 0x80 << ((i % 4) << 3);
if (i > 55) {
me.md5cycle(state, tail);
for (i = 0; i < 16; i++)
tail[i] = 0;
}
tail[14] = n * 8;
me.md5cycle(state, tail);
return state;
},

md5blk: function(s) {
var md5blks = [],
i;
for (i = 0; i < 64; i += 4) {
md5blks[i >> 2] = s.charCodeAt(i)
+ (s.charCodeAt(i + 1) << 8 )
+ (s.charCodeAt(i + 2) << 16)
+ (s.charCodeAt(i + 3) << 24);
}
return md5blks;
},

hex_chr: '0123456789abcdef'.split(''),

rhex: function(n) {
var s = '',
j = 0;
for (; j < 4; j++)
s += this.hex_chr[(n >> (j * 8 + 4)) & 0x0F]
+ this.hex_chr[(n >> (j * 8)) & 0x0F];
return s;
},

hex: function(x) {
for (var i = 0; i < x.length; i++)
x[i] = this.rhex(x[i]);
return x.join('');
},

md5: function(s) {
return this.hex(this.md51(s));
},

add32: function(a, b) {
return (a + b) & 0xFFFFFFFF;
}

})

Include in app.js


requires: [ 'MyApp.util.Crypto']

Use like this:


alert(Crypto.md5('hello'));