Character Check using Blockly

[XML] [Smart Pascal] [JavaScript] [Python] [output]

This is a demonstration of a character check of a string. It ensures in this case that a surname contains no characters other than letters, - and '. It is based on our Pascal page. We used the Blockly Playground to generate JavaScript and Python code from the same blocks.

The blocks

The blocks

XML Code of Blocks

<xml xmlns="http://www.w3.org/1999/xhtml">
  <block type="variables_set" id="bdL{U.z?}[!+.+tTti3)" x="13" y="13">
    <field name="VAR">strSurnameCharacters</field>
    <value name="VALUE">
      <block type="text" id="T#T=(c=J.N`c/qAJNhoH">
        <field name="TEXT">abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-'</field>
      </block>
    </value>
    <next>
      <block type="variables_set" id="dq*hQw?B`)_]89W9t0M}">
        <field name="VAR">boolCharacterCheckOK</field>
        <value name="VALUE">
          <block type="logic_boolean" id="P3,K=#)wLe0t|$1s;K_v">
            <field name="BOOL">FALSE</field>
          </block>
        </value>
        <next>
          <block type="controls_whileUntil" id="{r??5/6Y!_X4xJcm7V7@">
            <field name="MODE">WHILE</field>
            <value name="BOOL">
              <block type="logic_negate" id="eanl_+Tu)n$_..VTj+A3">
                <value name="BOOL">
                  <block type="variables_get" id="})![:Idmq_G};vDXBx{b">
                    <field name="VAR">boolCharacterCheckOK</field>
                  </block>
                </value>
              </block>
            </value>
            <statement name="DO">
              <block type="variables_set" id="wwWh$J,VDl}-pv~ib#f)">
                <field name="VAR">boolCharacterCheckOK</field>
                <value name="VALUE">
                  <block type="logic_boolean" id="J3sYm?`7rIc4tMfE!94b">
                    <field name="BOOL">TRUE</field>
                  </block>
                </value>
                <next>
                  <block type="variables_set" id="nMzf+AwtYG}^(5G9;_EY">
                    <field name="VAR">strSurname</field>
                    <value name="VALUE">
                      <block type="text_prompt_ext" id="JbB~h#1s7rqtMBbteT[j">
                        <mutation type="TEXT"></mutation>
                        <field name="TYPE">TEXT</field>
                        <value name="TEXT">
                          <shadow type="text" id="p07mLF|W[n5@A+@l0iJw">
                            <field name="TEXT">Please enter your surname.</field>
                          </shadow>
                        </value>
                      </block>
                    </value>
                    <next>
                      <block type="controls_for" id=";owzXx(:=}.ayrwWedhx">
                        <field name="VAR">i</field>
                        <value name="FROM">
                          <shadow type="math_number" id="a@,EFDJ9JB,)rEUM]~FR">
                            <field name="NUM">1</field>
                          </shadow>
                        </value>
                        <value name="TO">
                          <shadow type="math_number" id="B^72{Tj{r~YFFxA%)9j(">
                            <field name="NUM">10</field>
                          </shadow>
                          <block type="text_length" id="(iptLr)h=|5E[H878lz.">
                            <value name="VALUE">
                              <shadow type="text" id="Huj8.aD8MtH(8GKYnD!K">
                                <field name="TEXT">abc</field>
                              </shadow>
                              <block type="variables_get" id="mCq)3v-yLo;Z!=pF-yUk">
                                <field name="VAR">strSurname</field>
                              </block>
                            </value>
                          </block>
                        </value>
                        <value name="BY">
                          <shadow type="math_number" id="AD5_kI_-8ofX2h;u?`I?">
                            <field name="NUM">1</field>
                          </shadow>
                        </value>
                        <statement name="DO">
                          <block type="variables_set" id="1XtX1*wJFj*p-IwyLwZT">
                            <field name="VAR">strCurrentChar</field>
                            <value name="VALUE">
                              <block type="text_charAt" id="Vs=o|du*wA_^YwnHQEm2">
                                <mutation at="true"></mutation>
                                <field name="WHERE">FROM_START</field>
                                <value name="VALUE">
                                  <block type="variables_get" id="dHZ,I_`V:i;1#l]0,D5M">
                                    <field name="VAR">strSurname</field>
                                  </block>
                                </value>
                                <value name="AT">
                                  <block type="variables_get" id="Nl3|~F}Kb(x=+^MMXDB0">
                                    <field name="VAR">i</field>
                                  </block>
                                </value>
                              </block>
                            </value>
                            <next>
                              <block type="variables_set" id="!{Yfc`k+mJ`V6,(s]yJI">
                                <field name="VAR">intPos</field>
                                <value name="VALUE">
                                  <block type="text_indexOf" id=".T-FpWEd{QmH+ld7!E.k">
                                    <field name="END">FIRST</field>
                                    <value name="VALUE">
                                      <block type="variables_get" id="wXx3x5,#z?:Ty}{!JL-@">
                                        <field name="VAR">strSurnameCharacters</field>
                                      </block>
                                    </value>
                                    <value name="FIND">
                                      <shadow type="text" id=")pnd;(*m(c$.}4m7p;00">
                                        <field name="TEXT">abc</field>
                                      </shadow>
                                      <block type="variables_get" id="%_xqvi)Pq{PX2KhY0+xS">
                                        <field name="VAR">strCurrentChar</field>
                                      </block>
                                    </value>
                                  </block>
                                </value>
                                <next>
                                  <block type="controls_if" id="7HNLOzF_SB_[-(Q{Mo?1">
                                    <value name="IF0">
                                      <block type="logic_compare" id="$pAo!QM67%/Z9m![nxID">
                                        <field name="OP">LT</field>
                                        <value name="A">
                                          <block type="variables_get" id="7mu_k@Rx4|2H8]?Wq6i|">
                                            <field name="VAR">intPos</field>
                                          </block>
                                        </value>
                                        <value name="B">
                                          <block type="math_number" id="?Nr:ks*rDl~NQL2TCs@^">
                                            <field name="NUM">1</field>
                                          </block>
                                        </value>
                                      </block>
                                    </value>
                                    <statement name="DO0">
                                      <block type="variables_set" id="v)#QlSz@JFKQkg$b@~Zz">
                                        <field name="VAR">boolCharacterCheckOK</field>
                                        <value name="VALUE">
                                          <block type="logic_boolean" id="XL#UFzNG=*:Dxu6_aL](">
                                            <field name="BOOL">FALSE</field>
                                          </block>
                                        </value>
                                      </block>
                                    </statement>
                                  </block>
                                </next>
                              </block>
                            </next>
                          </block>
                        </statement>
                      </block>
                    </next>
                  </block>
                </next>
              </block>
            </statement>
            <next>
              <block type="text_append" id="f%@3eMj,fZaD:3U,MCHM">
                <field name="VAR">strSurname</field>
                <value name="TEXT">
                  <shadow type="text" id="|}EE;{z)b}GMM#rO]JlM">
                    <field name="TEXT"> accepted</field>
                  </shadow>
                </value>
                <next>
                  <block type="text_print" id="UYXVd5Stleh=Qxg5k6$J">
                    <value name="TEXT">
                      <shadow type="text" id="$$pAj~4NxcZzrfvdLS3-">
                        <field name="TEXT">abc</field>
                      </shadow>
                      <block type="variables_get" id="z-epoN|nagh{?pmIUZf}">
                        <field name="VAR">strSurname</field>
                      </block>
                    </value>
                  </block>
                </next>
              </block>
            </next>
          </block>
        </next>
      </block>
    </next>
  </block>
</xml>

Generated Smart Pascal Code

var strSurname: String;
var boolCharacterCheckOK: Boolean;
var intPos: Integer;
var strCurrentChar: String;
var strSurnameCharacters: String;



strSurnameCharacters := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-''';
boolCharacterCheckOK := false;
while ( not boolCharacterCheckOK) do
begin
  boolCharacterCheckOK := true;
  strSurname := prompt('Please enter your surname.');
  for var i := 1 to length(strSurname) step 1 do
  begin
    strCurrentChar := CharAt(strSurname, (i - 1) + 1);
    intPos := Pos(strCurrentChar, strSurnameCharacters);
    if ((intPos < 1)) then
    begin
      boolCharacterCheckOK := false;
    end;
  end;
end;
strSurname += ' accepted';
Console.writeln(strSurname);
   

Generated JavaScript

var strSurname, boolCharacterCheckOK, intPos, strCurrentChar, strSurnameCharacters, i;


strSurnameCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-\'';
boolCharacterCheckOK = false;
while (!boolCharacterCheckOK) {
  boolCharacterCheckOK = true;
  strSurname = window.prompt('Please enter your surname.');
  var i_end = strSurname.length;
  var i_inc = 1;
  if (1 > i_end) {
    i_inc = -i_inc;
  }
  for (i = 1; i_inc >= 0 ? i <= i_end : i >= i_end; i += i_inc) {
    strCurrentChar = strSurname.charAt((i - 1));
    intPos = strSurnameCharacters.indexOf(strCurrentChar) + 1;
    if (intPos < 1) {
      boolCharacterCheckOK = false;
    }
  }
}
strSurname = String(strSurname) + String(' accepted');
window.alert(strSurname);

Generated Python Code

strSurname = None
boolCharacterCheckOK = None
intPos = None
strCurrentChar = None
strSurnameCharacters = None
i = None

def text_prompt(msg):
  try:
    return raw_input(msg)
  except NameError:
    return input(msg)

def upRange(start, stop, step):
  while start <= stop:
    yield start
    start += abs(step)

def downRange(start, stop, step):
  while start >= stop:
    yield start
    start -= abs(step)


strSurnameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-'"
boolCharacterCheckOK = False
while not boolCharacterCheckOK:
  boolCharacterCheckOK = True
  strSurname = text_prompt('Please enter your surname.')
  i_end = float(len(strSurname))
  for i in (1 <= i_end) and upRange(1, i_end, 1) or downRange(1, i_end, 1):
    strCurrentChar = strSurname[int(i - 1)]
    intPos = strSurnameCharacters.find(strCurrentChar) + 1
    if intPos < 1:
      boolCharacterCheckOK = False
strSurname = str(strSurname) + str(' accepted')
print(strSurname)

Copy of Output

The output on attempting to enter W9lliams, [eters, :every and O'Shea was:

O'Shea accepted
Programming - a skill for life!

Introduction to validation (length check, range check, presence check, type check and character check)