admin管理员组文章数量:1244421
How can i Valdiate my TextField in Titanium or in JavaScript to restrict it to numbers only.
var txt_appt2 = Titanium.UI.createTextField({
top:2,
left:240,
width:75,
color:'#000',
backgroundColor:'#fff',
font: {fontSize: 12}
});
How can i Valdiate my TextField in Titanium or in JavaScript to restrict it to numbers only.
var txt_appt2 = Titanium.UI.createTextField({
top:2,
left:240,
width:75,
color:'#000',
backgroundColor:'#fff',
font: {fontSize: 12}
});
Share
Improve this question
asked Apr 7, 2011 at 7:36
theJavatheJava
15k48 gold badges134 silver badges174 bronze badges
3 Answers
Reset to default 9txt_appt2.addEventListener('change',function(e){
txt_appt2.value = txt_appt2.value.replace(/[^0-9]+/,"");
});
Add
keyboardType:Titanium.UI.KEYBOARD_NUMBER_PAD,
to the TextField.
See example at http://www.lonhosford./lonblog/2011/04/06/titanium-limit-the-characters-in-a-textfield/
For those wondering why they are experiencing continuous loops and errors;
The problem isn't listening to the onChange event. That's the proper event since it fires every time the value is changed. I.e. Copy and paste, keypress and so on.
On iOS users can copy and paste in characters even though you're only limiting to decimal / numeric keyboard.
Avoid trying to set the field value directly by referencing the text field property itself. Instead, use the text field property returned when the text field is changed. Doing it this way won't cause the onChange event to keep firing causing a never-ending loop.
// XML
<TextField keyboardType="Ti.UI.KEYBOARD_TYPE_DECIMAL_PAD" value="0.00" onChange="Alloy.Globals.helper.decimalFormat" />
// Alloy.js
Alloy.Globals.helper = {
decimalFormat: function(e) {
// Strip all characters from the input except digits
var input = e.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');
e.source.value = input;
}
};
本文标签: javascriptTextField Valdiation in Appcelerator TitaniumStack Overflow
版权声明:本文标题:javascript - TextField Valdiation in Appcelerator Titanium - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740184979a2238036.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论