I'm beggining to use B4JS and I think it's really useful to validate the fields of a form by using the events KeyUp and LostFocus and I love that it gets done on client side. I want to create a very generic function that will be used on the event LostFocus of any input. this is my code:
this is in a normal B4J class. this is my B4JS class (test_b4js):
when I run it, the console always shows the "error" Log() even if I only write numbers, and the other Log() that is supposed to show the "val" variable always shows "value: undefined". But if I do this:
it works without problems. Can't I assign an unique key with a string that comes from a parameter? or am I doing something wrong?
I thank you beforehand.
B4X:
Dim myInp As ABMInput
myInp.Initialize(page, "myInput", ABM.INPUT_TEXT, "Numbers", False, "")
myInp.B4JSUniqueKey = "key-numbers"
myInp.B4JSOnLostFocus("test_b4js", "testNumber", Array As Object("key-numbers"))
page.Cell(6,1).AddComponent(myInp)
this is in a normal B4J class. this is my B4JS class (test_b4js):
B4X:
'Class module
Sub Class_Globals
' use public or dim if you want to share this variable over ALL B4JS classes
' use private if only within this class
' to access the constants
Public ABM As ABMaterial 'ignore
' so we can use an msgbox
Public Page As ABMPage 'ignore, just to be able to run ABMPage functions
End Sub
'Initializes the object. You can NOT add parameters to this method.
'MUST be called InitializeB4JS is automatically called when using this class
Public Sub InitializeB4JS
End Sub
Public Sub testNumber(uniqueKey As String)
Dim inp As ABMInput 'ignore
inp.B4JSUniqueKey = uniqueKey
Dim val As String = inp.B4JSText
Log("value: "&val)
Dim isValid As Boolean = Page.B4JSRunInlineJavascriptMethod("validate", Array As Object(val, "^[0-9]+$"))
If isValid Then
Log("Only numbers")
Else
Log("Error")
End If
End Sub
#If JAVASCRIPT
function validate(value, expression) {
var reg_exp = new RegExp(expression);
if (reg_exp.test(value))
return true;
else
return false;
}
#End If
when I run it, the console always shows the "error" Log() even if I only write numbers, and the other Log() that is supposed to show the "val" variable always shows "value: undefined". But if I do this:
B4X:
inp.B4JSUniqueKey = "key-numbers"
it works without problems. Can't I assign an unique key with a string that comes from a parameter? or am I doing something wrong?
I thank you beforehand.