jQuery: suggerire automaticamente una password agli utenti

In jQuery possiamo suggerire automaticamente una password agli utenti.

jQuery di Gabriele Romanato

In jQuery possiamo suggerire automaticamente una password agli utenti.

La soluzione รจ la seguente:


$.password = function( length ) {
    length = length || 12;
    var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split( "" );
    var shuffled = chars.sort( function( a, b ) { return Math.random() - 0.5; } ).join( "" );
    return shuffled.substring( 0, length );
};
$( "#password" ).one( "focus", function() {
    var suggestedPwd = $.password();
    var $field = $( this );
    var $suggestionEl = $field.next();

    $suggestionEl.hide(); // Reset
    $suggestionEl.text( "Suggested password: " + suggestedPwd ).show();
});

Per non infastidire l'utente con continui suggerimenti, l'evento che regola l'azione ha luogo una sola volta.

Commenti