/*!
* Copyright (c) 2009 Francesco Mele jsbeans@francescomele.com
*
* This Software is licenced under the LGPL Licence (GNU Lesser General
* Public License).
* In addition to the LGPL Licence the Software is subject to the
* following conditions:
*
* i every modification must be public and comunicated to the Author
* ii every "jsbean" added to this library must be self consistent
* except for the dependence from jsbeans-x.x.x.js
* iii copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* Validator extensions for password strength.<br/>
* It tests for length (minimum 6 characters), case (both cases), numbers and characters mix, two sets of other characters (.+-_! and |$&()?'^[]{}#,;:)<br/>
* Each of them has a weight:
* <pre>
* length: 0.3
* case: 0.25
* mix: 0.25
* a set of other chars: 0.1 each
* ---------------------------------
* of course the sum is 1
* A strong password has at least 0.6. This means the password has to check at least 3 of provided controls.
* </pre>
* @for Validator
* @namespace jsbeans
* @static
*
*/
jsbeans.Validator.passwordStrength = {
/**
* Default messages for Validator.passwordStrength extension
* @property passwordStrength.messages
* @type JSON
* @static
* */
messages: {
en: "is a week password. Try adding numbers, changing case or characters different from letters and digits.",
it: "e' una password debole. Prova ad aggiungere numeri, combinare maiuscole e minuscole o aggiungendo caratteri diversi da lettere e numeri"
},
/**
* See {@param} <code class="param">jsbeans.Validator.passwordStrength</code> for details.
* @method assertPasswordStrength
* @param input {DOM} the input to validate
* @return {boolean} true if at least 0.6 is reached as every control has a weight.
* @static
* */
assertPasswordStrength: function(obj, arg) {
var val = (obj.value || "").replace(/^\s+|\s+$/g, "");
if (val == "") {
return true;
}
var res = 0;
if (val.length >= 6) {
res += 0.3;
}
if (/[a-z]/.test(val) && /[A-Z]/.test(val)) {
res += 0.25;
}
if (/[0-9]/.test(val)) {
res += 0.25;
}
if ( val.indexOf(".") != -1
|| val.indexOf("+") != -1
|| val.indexOf("-") != -1
|| val.indexOf("_") != -1
|| val.indexOf("!") != -1) {
res += 0.1;
}
if ( val.indexOf("|") != -1
|| val.indexOf("$") != -1
|| val.indexOf("&") != -1
|| val.indexOf("(") != -1
|| val.indexOf(")") != -1
|| val.indexOf("'") != -1
|| val.indexOf("^") != -1
|| val.indexOf("[") != -1
|| val.indexOf("]") != -1
|| val.indexOf("{") != -1
|| val.indexOf("}") != -1
|| val.indexOf("#") != -1
|| val.indexOf(",") != -1
|| val.indexOf(";") != -1
|| val.indexOf(":") != -1) {
res += 0.1;
}
return (res >= 0.6);
}
};