Wednesday, April 22, 2009

PL/SQL code to validate a Greek VAT Number (ΑΦΜ)

FUNCTION check_afm (v_afm VARCHAR2)
      RETURN PLS_INTEGER
   AS
      resultvalid   PLS_INTEGER := 0;
      l_digit       PLS_INTEGER;
      s             PLS_INTEGER := 0;
      m             PLS_INTEGER;
   BEGIN
      IF LENGTH (v_afm) != 9
      THEN
         RETURN 0;
      END IF;

      IF v_afm = '000000000'
      THEN
         RETURN 0;
      END IF;

      IF    REGEXP_SUBSTR (v_afm, '\d{9}') IS NULL
         OR REGEXP_SUBSTR (v_afm, '\d{9}') != v_afm
      THEN
         RETURN 0;
      END IF;

      FOR i IN 1 .. 8
      LOOP
         l_digit := TO_NUMBER (SUBSTR (v_afm, i, 1));
--        DBMS_OUTPUT.PUT_LINE ( 'l_digit = ' || l_digit );
         s := s + l_digit * POWER (2, 9 - i);
--        DBMS_OUTPUT.PUT_LINE('s = ' || s );
      END LOOP;

      m := MOD (s, 11);

--    DBMS_OUTPUT.PUT_LINE('s = ' || s );
      IF (m = 10)
      THEN
         m := 0;
      END IF;

      IF m = ASCII (SUBSTR (v_afm, 9, 1)) - 48
      THEN
         RETURN 1;
      END IF;

      RETURN 0;
   END;






Friday, April 10, 2009

Prevent users from hitting a submit button twice in a JSF application

I had this small JSF application and I was trying to implement something to prevent users from hitting a submit button twice.

Initially I tried to use Javascript to disable the button (from the onclick of the button).

However, when the button was disabled, the form was not submitted.

I found this from IBM and used it with a small change (added a “return” in the onclick event).

The overall concept is to implement a javascript function that detects that the submit button has been already clicked and call it from the onclick event of the form.

This is the javascript code:


<script>
var requestSubmitted = false;
       function submitRequest() {
           if (!requestSubmitted ) {
                     requestSubmitted  = true;
                    return true;
                }

              alert('Already submitted...')
              return false;
        }
</script>


And this is how it is invoked from the form :

<h:form id="searchForm" onsubmit="javascript:return submitRequest();">

Of course the button is not disabled, but at least the form is not being re-submitted.