I have a form on my app that I am trying to force a particular format on the text field (ex. dob 11/33/1944). The following code works great on iPhone but on Android the app stops working once I enter the first number. Can someone please help me modify the code so that it works for both iPhone and Android. Thanks for the help.
I have a file called global.js where I have the code that forces the format. This is what I am using:
Mask =
{
mask: function(_field, _function)
{
_field.value = _function(_field.value);
},
dob: function(v)
{
v = v.replace(/\D/g,"");
v = v.replace(/^(\d\d)(\d)/g,"$1/$2");
v = v.replace(/(\d{2})(\d)/,"$1/$2");
return v.slice(0, 10);
},
phone: function(v)
{
v = v.replace(/\D/g,"");
v = v.replace(/^(\d\d)(\d)/g,"($1) $2");
v = v.replace(/(\d{4})(\d)/,"$1-$2");
return v.slice(0, 14);
}
};
On my form i added the following on the top of the of the view for the global.js:
var styles = require('global').Styles;
Then my field is as follows:
// Create a TextField.
var aTextField_dob = Ti.UI.createTextField(
{
top : "10%",
width : '80%',
height:'8%',
color: styles.textfield.color,
font :
{
fontSize : font_text
},
hintText : 'Date of Birth (Ex.01/01/1999)',
paddingLeft : styles.textfield.paddingLeft,
backgroundImage : styles.textfielda.backgroundImage,
});
// Listen for return events.
aTextField_dob.addEventListener('change', function(e)
{
Mask.mask(aTextField_dob, Mask.dob);
});
Here is the issue I get on the Android device, it just keeps repeating itself and the app has to be forced to stop:
[WARN] : IInputConnectionWrapper: getCursorCapsMode on inactive InputConnection
First of all, I’d avoid the issue altogether by using a date picker control. That’d be a better UI/UX anyway.
It’s probable that you are creating infinite recursion in the change event. That could be avoided by using the blur (lost focus) event, or the keypressed event if it can’t wait until focus is lost.
I’m not sure if the regular expression engines are different. Try putting the regex output into another control or just log it – separate it from any possible change event infinite loop to test that out.