This one tries to force 3 letters using a regular expression.
// Force a 3 character response
// for greater control you can check
// as you go along by using
// edt.SetOnChange()
// you can do what you like with edt.data
// in many cases you will need
// minLen and/or maxLen instead of reqLen
function OnStart()
{
var lay = app.CreateLayout("linear", "VCenter,FillXY");
var edt = app.CreateTextEdit("", -1, -1, "singleLine");
edt.SetHint("3 char country code");
edt.data.pattern=/^[A-Za-z]+$/;
edt.data.reqLen=3;
edt.SetOnEnter(validate);
lay.AddChild(edt);
app.AddLayout(lay);
}
function validate()
{
var value = this.GetText();
var reqLen=this.data.reqLen;
var len=value.length;
if(len===reqLen&&value.match(this.data.pattern))
{
app.Alert( value, "You chose" );;
}
else
{
app.ShowPopup(value + " is not valid");
this.SetText(value.slice(0,reqLen));
this.Focus();
app.ShowKeyboard(this);
}
}
It has to allow some invalid intermediate values while the user is typing such as a minus sign on its own but these are disallowed when pressing the Enter key.
// numeric check.
// checks as it gies along
function OnStart()
{
var lay = app.CreateLayout("linear", "VCenter,FillXY");
var edt = app.CreateTextEdit("", -1, -1, "number,singleLine");
edt.SetHint("Please enter a number");
edt.data.lastValid = "";
edt.SetOnChange(validate);
edt.SetOnEnter(finalCheck);
lay.AddChild(edt);
app.AddLayout(lay);
}
function validate()
{
var value = this.GetText();
if(value == "-") return
var ret = +value;
if(isNaN(ret))
{
app.ShowPopup(value + " is not a number");
this.SetText(this.data.lastValid);
this.SetCursorPos(this.data.lastValid.length);
}
else
{
this.data.lastValid = value;
}
}
// mostly check for "-" on its own
function finalCheck()
{
var value = this.GetText();
var ret = +value;
if(isNaN(ret))
{
app.ShowPopup(value + " is not a number");
this.SetText(this.data.lastValid);
this.SetCursorPos(this.data.lastValid.length);
this.Focus();
app.ShowKeyboard(this);
}
else
{
app.HideKeyboard();
app.Alert(ret, "You chose");
}
}