I am NOT looking for a plug-in, just need simple jQuery function with regexp. I am also NOT interested in CSS or PHP/SSI solutions.
Given that, I have the following snippet of code which is currently is not working. I wish to keep similar selector format, the regexp syntex has 开发者_Go百科been tested individually too. So, please look at the code and let me know what the problem is.
Code link: http://jsfiddle.net/TnpZY/1/
Currernt Code:
$('input').live('keyup', function(e) {
var val = this.value;
var defaults = {
format: 'numeric',
uppercase: false,
lowercase: false,
capit: false,
nospace: false,
pattern: null
};
switch(defaults.format)
{
case 'text':
var pattern = new RegExp('[0-9]+', 'g');
val = val.replace(pattern, '');
break;
case 'alpha':
var pattern = new RegExp('[^a-zA-Z]+', 'g');
val = val.replace(pattern, '');
break;
case 'number':
case 'numeric':
var pattern = new RegExp('[^0-9]+', 'g');
val = val.replace(pattern, '');
break;
case 'alphanumeric':
var pattern = new RegExp('[^0-9a-zA-Z]+', 'g');
val = val.replace(pattern, '');
break;
case 'custom':
var pattern = new RegExp(defaults.pattern, 'g');
val = val.replace(pattern, '');
break;
case 'all':
default:
if(typeof defaults.format == 'function')
var val = defaults.format(val);
break;
}
if(defaults.nospace)
{
var pattern = new RegExp('[ ]+', 'g');
val = val.replace(pattern, '');
}
if(defaults.uppercase)
val = val.toUpperCase();
if(defaults.lowercase)
val = val.toLowerCase();
if(defaults.capit)
String.prototype.capitalizet = function(){ return this.charAt(0).toUpperCase() + this.slice(1); }
val = val.capitalizet();
if(val != this.value)
this.value = val;
});
Your capitalizet function was causing the problem. See here.
if(defaults.capit)
String.prototype.capitalizet = function(){ return this.charAt(0).toUpperCase() + this.slice(1); }
val = val.capitalizet();
should be
if(defaults.capit) {
String.prototype.capitalizet = function(){ return this.charAt(0).toUpperCase() + this.slice(1); }
val = val.capitalizet();
}
精彩评论