Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Format or validate textfield (or textarea) as soft keys are being pressed with keypress

$
0
0

Since there is no keypress event in titanium, I thought I would share this hack I used to make sure that a credit card number had a space between each 4 numbers as it was being typed.

You can re-work this general concept to use for various validation and formatting as the user is typing.

var textField = Ti.UI.createTextField();
 
var myInterval = false;
 
function formatCC() {
    var str = textField.value.replace(/[^\d]/g,'');
    var i = 0;
    var newStr = '';
    while( str[i] !== undefined ) {
        newStr += ''+str[i];
        if ( (i+1) % 4 == 0 ) {
            newStr += ' ';
        }
 
        i++;
    }
 
    textField.value = newStr;
}
 
textField.addEventListener('focus', function(e) {
    myInterval = setInterval(function() {
        formatCC()
    },250);
 
});
 
textField.addEventListener('blur', function(e) {
    clearInterval(myInterval);
});
Hope this helps someone!

Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>