Last active
August 29, 2015 14:09
-
-
Save diegocasmo/6ed4bf566ccb05f09db8 to your computer and use it in GitHub Desktop.
Dynamic validation helper.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Validator Class | |
*/ | |
define([ | |
'jquery', | |
'underscore' | |
], function($, _){ | |
var Validator = { | |
noSpecials: function(event) { | |
var regex = new RegExp("^[a-zA-Z0-9]+$"); | |
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode); | |
var keycode = event.keyCode ? event.keyCode : event.charCode; | |
if ((!regex.test(key)) && (keycode != 8) && (keycode != 9)) { | |
event.preventDefault(); | |
return false; | |
} | |
}, | |
noBlankSpace: function(event) { | |
var code = (event.keyCode ? event.keyCode : event.which); | |
if (code == 32) { | |
event.preventDefault(); | |
return false; | |
} | |
}, | |
noNumbers: function(event) { | |
var a = []; | |
var k = event.which; | |
if ($.browser.msie && ($.browser.version < 9)) { | |
var k = event.keyCode ? event.keyCode : event.charCode; | |
if (k >= 48 && k <= 57) | |
event.preventDefault(); | |
} else { | |
for (i = 48; i < 58; i++) | |
a.push(i); | |
if ((a.indexOf(k) >= 0)) | |
event.preventDefault(); | |
} | |
}, | |
onlyNumbers: function(event) { | |
var char = (event.which) ? event.which : event.keyCode | |
if (char > 31 && (char < 48 || char > 57)) { | |
event.preventDefault(); | |
return false; | |
} | |
return true; | |
}, | |
onlyLoweCase: function(event) { | |
var char = (event.which) ? event.which : event.keyCode; | |
string = String.fromCharCode(char); | |
if(string.match(/[A-Z]/)) { | |
event.preventDefault(); | |
return false; | |
} | |
return true; | |
} | |
} | |
return Validator; | |
}); | |
/** | |
* Usage on View | |
*events: { | |
* 'keypress .no-scpecials': function(event){ | |
* Validator.noSpecials(event); | |
* } | |
*}, | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment