# Add a JavaScript function to the xml source code

**URL:** <https://forum.dimagi.com/t/add-a-javascript-function-to-the-xml-source-code/2370>\
**Category:** Users\
**Created:** [August 27, 2015, 5:50am UTC](https://forum.dimagi.com/t/add-a-javascript-function-to-the-xml-source-code/2370 "2015-08-27T05:50:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rob1](https://dimagi.b-cdn.net/letter_avatar_proxy/v4/letter/r/e495f1/32.png) [@Rob1](https://forum.dimagi.com/u/Rob1)\
**Post date:** [August 27, 2015, 5:50am UTC](https://forum.dimagi.com/t/add-a-javascript-function-to-the-xml-source-code/2370/1 "2015-08-27T05:50:38Z")

</div>

Hi. Is it possible to add a JavaScript function to the xml source code?

With Thanks  
Rob

---

<div class="post-metadata">

**Author:** ![Rob1](https://dimagi.b-cdn.net/letter_avatar_proxy/v4/letter/r/e495f1/32.png) [@Rob1](https://forum.dimagi.com/u/Rob1)\
**Post date:** [August 27, 2015, 12:40pm UTC](https://forum.dimagi.com/t/add-a-javascript-function-to-the-xml-source-code/2370/2 "2015-08-27T12:40:15Z")

</div>

I',m trying to validate RSA ID Nos. The code includes things like the  
length (13) and a valid date in the first 6 numbers. But there's also a  
formula to calculate the validity of the last no. The code in VBA is:

Public Function IDValid(ByVal varIdNo As Variant) As Boolean  
On Error GoTo ErrHandler  
Dim idNo As String  
Dim a As Integer, b As Long, c As Integer, d As Integer, i As Integer

```
If IsNull(varIdNo) Then
    IDValid = False
    Exit Function
Else
    idNo = CStr(varIdNo)
End If
idNo = RemoveSpaces(idNo)
If Len(idNo) = 13 Then

    a = 0
    b = 0
    c = 0
    d = 0
    i = 0
    For i = 0 To 5
        a = a + CInt(Mid(idNo, i * 2 + 1, 1))
    Next

    For i = 0 To 5
        b = b * 10 + CInt(Mid(idNo, i * 2 + 2, 1))
    Next
    b = b * 2

    Do
        c = c + b Mod 10
        b = Int(b / 10)
    Loop Until b <= 0
    c = c + a
    
    d = 10 - (c Mod 10)
    If (d = 10) Then d = 0
    If (d = CInt(Mid(idNo, 13, 1))) And (IsDate("19" & Mid(idNo, 1, 2) 

```

& "/" & Mid(idNo, 3, 2) & "/" & Mid(idNo, 5, 2)) Or IsDate("20" & Mid(idNo,  
1, 2) & "/" & Mid(idNo, 3, 2) & "/" & Mid(idNo, 5, 2))) Then  
IDValid = True  
Else  
IDValid = False  
End If  
Else  
IDValid = False  
End If  
ExitProc:  
On Error Resume Next  
Exit Function  
ErrHandler:  
'Fail quietly  
Resume ExitProc  
Resume  
End Function

---

<div class="post-metadata">

**Author:** ![czue](https://dimagi.b-cdn.net/user_avatar/forum.dimagi.com/czue/32/493_2.png) [@czue](https://forum.dimagi.com/u/czue)\
**Post date:** [August 27, 2015, 12:05pm UTC](https://forum.dimagi.com/t/add-a-javascript-function-to-the-xml-source-code/2370/3 "2015-08-27T12:05:15Z")

</div>

Hello,

Unfortunately no. You only have access to the built in logic  
[https://confluence.dimagi.com/display/commcarepublic/Common+Logic+and+Calculations](https://confluence.dimagi.com/display/commcarepublic/Common+Logic+and+Calculations)  
and functions  
[https://confluence.dimagi.com/display/commcarepublic/CommCare+Functions](https://confluence.dimagi.com/display/commcarepublic/CommCare+Functions).

If you have a specific use case for a javascript function you might be able  
to articulate it here and someone could help you figure out a workaround.

thanks,  
Cory

> **···**
>
> On Thu, Aug 27, 2015 at 1:50 AM, iotc iotc wrote:
> 
> > Hi. Is it possible to add a JavaScript function to the xml source code?
> > 
> > With Thanks  
> > Rob
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "commcare-users" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [commcare-users+unsubscribe@googlegroups.com](mailto:commcare-users+unsubscribe@googlegroups.com).  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![czue](https://dimagi.b-cdn.net/user_avatar/forum.dimagi.com/czue/32/493_2.png) [@czue](https://forum.dimagi.com/u/czue)\
**Post date:** [August 27, 2015, 7:02pm UTC](https://forum.dimagi.com/t/add-a-javascript-function-to-the-xml-source-code/2370/4 "2015-08-27T19:02:26Z")

</div>

Hey,

I see. I think you can get pretty close to this with a regex but probably  
not the whole way there.

Cory

> **···**
>
> On Thu, Aug 27, 2015 at 8:40 AM, Rob wrote:
> 
> > I',m trying to validate RSA ID Nos. The code includes things like the  
> > length (13) and a valid date in the first 6 numbers. But there's also a  
> > formula to calculate the validity of the last no. The code in VBA is:
> > 
> > Public Function IDValid(ByVal varIdNo As Variant) As Boolean  
> > On Error GoTo ErrHandler  
> > Dim idNo As String  
> > Dim a As Integer, b As Long, c As Integer, d As Integer, i As Integer
> > 
> > ```
> > If IsNull(varIdNo) Then
> > IDValid = False
> > Exit Function
> > Else
> > idNo = CStr(varIdNo)
> > End If
> > idNo = RemoveSpaces(idNo)
> > If Len(idNo) = 13 Then
> > 
> > a = 0
> > b = 0
> > c = 0
> > d = 0
> > i = 0
> > For i = 0 To 5
> > a = a + CInt(Mid(idNo, i * 2 + 1, 1))
> > Next
> > 
> > For i = 0 To 5
> > b = b * 10 + CInt(Mid(idNo, i * 2 + 2, 1))
> > Next
> > b = b * 2
> > 
> > Do
> > c = c + b Mod 10
> > b = Int(b / 10)
> > Loop Until b <= 0
> > c = c + a
> > 
> > d = 10 - (c Mod 10)
> > If (d = 10) Then d = 0
> > If (d = CInt(Mid(idNo, 13, 1))) And (IsDate("19" & Mid(idNo, 1, 2)
> > 
> > ```
> > 
> > & "/" & Mid(idNo, 3, 2) & "/" & Mid(idNo, 5, 2)) Or IsDate("20" & Mid(idNo,  
> > 1, 2) & "/" & Mid(idNo, 3, 2) & "/" & Mid(idNo, 5, 2))) Then  
> > IDValid = True  
> > Else  
> > IDValid = False  
> > End If  
> > Else  
> > IDValid = False  
> > End If  
> > ExitProc:  
> > On Error Resume Next  
> > Exit Function  
> > ErrHandler:  
> > 'Fail quietly  
> > Resume ExitProc  
> > Resume  
> > End Function
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "commcare-users" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [commcare-users+unsubscribe@googlegroups.com](mailto:commcare-users+unsubscribe@googlegroups.com).  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
