writing a jquery plugin-- giving blank value for my matched textboxes "inpu1.val() - giving balnak values"
hi,
below is my plugin and its usuage but its always giving a blank values for "inpu1.val()".
its working fine in individual files but not working in my application site.
even its attached mouse events with my matched textboxes but values are coming as null.
usuage:
$("#subscriberZip").placeCharacter({mask:'99999-||||'});
plugin:
/*
* jQuery custom plugin for optional masking
* how to use:
* <input type="text" class="tp" name="t1" id="t1" value="">
* $(".tp").placeCharacter({mask:'99999-||||'});
* "9"- for numeric characters
* "a"- for alpha characters
* "|"- for optional characters
*/
//an anonymous function to avoid conflict
;(function($){
$.fn.extend({
placeCharacter: function(settings) {
var defaults = {
mask: ''
}
if (settings){
//alert ('pass1');
$.extend(defaults, settings);
}
//Iterate over the current set of matched elements
return this.each(function() {
alert ('in loop');
var regExpInt = /^\d{1}$/;
var regExpAlpha = /[a-z]/;
var ctr = 0;
var inpu1 = $(this);
var k = "";
var ignore = false;
//bind keypress event
inpu1.bind(
"keypress",
function(e)
{
e = e || window.event;
k = e.keyCode || e.charCode || e.which;
var kChar = String.fromCharCode(k);
///////// my BUG MY BUG MY BUG MY BUG,
/// this is (inpu1.val().length) always coming as ZERO
var keyCount = inpu1.val().length;
////////////////////////
tmpStr = "";
keyCount++;
if (keyCount >defaults.mask.length){ignore=true;}//ignore keys
if (k ==8 || k ==9){return true;}
if (ignore){return false;}
switch (keyCount)
{
default:
tmpStr = "";
maskChar = defaults.mask.charAt(keyCount-1);
if (maskChar == '9'){ //numeric characters
result = regExpInt.test(kChar);
if (result){}
else {return false;}
}
else if (maskChar == 'a'){ //alpha characters
result = regExpAlpha.test(kChar);
if (result){}
else {return false;}
}
else if (maskChar == '|'){ alert ('kp|');return true; } //optional characters
else { //masking characters
tmpStr = inpu1.val();
inpu1.val(tmpStr+maskChar+kChar);
return false;
}
}//switch close
}//function keypress close
);//bind function close
inpu1.bind(
"keydown",
function(e)
{
e = e || window.event;
k = e.keyCode || e.charCode || e.which;
if (k >=33 && k <=40){
ignore=true;
return false;
}
else{
ignore=false;
}
}
);
}); //close return loop of matched elements
}///function close
});//extend close
})(jQuery); ///function close