test regex
by tsamigr
HTML
<input class="form-control inp-lft text-box single-line" data-placement="bottom" data-toggle="tooltip" data-val="true" da id="SchoolIdentificationName" placeholder="e.g. myschool21" title="" value="" type="text">
<div id="debug">
</div>
JavaScript
function htmllog(msg) {
jQuery("#debug").prepend(msg + "<br/>");
}
/*jQuery('#SchoolIdentificationName').on('keyup', function(e) {
var englishAlphabetAndWhiteSpace = /[a-z0-9-]/g;
var code = e.keyCode || e.which;
var key = String.fromCharCode(code).toLowerCase();
htmllog(code);
if (code == 9 || code == 8 || code == 37 || code == 39 || englishAlphabetAndWhiteSpace.test(key)) {
return true;
}
return false;
});*/
/*jQuery('#SchoolIdentificationName').on('keydown', function(e) {
var englishAlphabetAndWhiteSpace = /[a-zA-Z0-9-]/g;
var code = e.keyCode || e.which;
htmllog(code);
var key = String.fromCharCode(code);
htmllog(key);
if (code == 9 || code == 8 || code == 37 || code == 39 || englishAlphabetAndWhiteSpace.test(key)) {
var schoolIdName = jQuery("#SchoolIdentificationName");
schoolIdName.val(schoolIdName.val().toLowerCase());
return true;
}
e.preventDefault();
var schoolIdName = jQuery("#SchoolIdentificationName");
schoolIdName.val(schoolIdName.val().toLowerCase());
return false;
});*/
$.fn.blockInput = function(options) {
// find inserted or removed characters
function findDelta(value, prevValue) {
var delta = '';
for (var i = 0; i < value.length; i++) {
var str = value.substr(0, i) +
value.substr(i + value.length - prevValue.length);
if (str === prevValue) delta =
value.substr(i, value.length - prevValue.length);
}
return delta;
}
function isValidChar(c) {
return new RegExp(options.regex).test(c);
}
function isValidString(str) {
for (var i = 0; i < str.length; i++)
if (!isValidChar(str.substr(i, 1))) return false;
return true;
}
this.filter('input,textarea').on('input', function() {
var val = this.value,
lastVal = $(this).data('lastVal');
// get inserted chars
var inserted = findDelta(val, lastVal);
// get removed chars
var removed = findDelta(lastVal, val);
// determine if user pasted content
...