Hi,

Ik hope someone can help help me. I found some php-code that I'd like to use for validating user input in a 'social insurance number field', but I don't now how to addapt the code for use with covert_fields.

The code:

/**
 * Checks whether or not the sofinummer provided passes the test of 11.
 * @version 2.0
 * @param $sofinummer The sofinummer to be checked.
 * @return boolean
 * @author Ivo Peters
 * @author Berry Langerak, al had Ivo hem als eerste goed :)
 **/
function isSofinummer( $sofinummer ) {
  $snummer = trim( $sofinummer ); 
  // lijst met nummers die qua check kloppen, maar toch niet geldig zijn
  $aInvalid = array( '111111110', 
                    '999999990', 
                    '000000000' );
  if( strlen( $snummer ) != 9 || !ctype_digit( $snummer ) || in_array( $snummer, $aInvalid ) ) {
    return false;
  }
  for( $i = 9, $som = -$snummer % 10; $i > 1; $i-- ) {
    $som += $i * $snummer{( 9 - $i )};
  }
  return ( $som % 11 == 0 );
}

Thanks...

Comments

guypaddock’s picture

Try this in the "Code for Validation:

  if (!function_exists('isSofinummer')) {
    /**
    * Checks whether or not the sofinummer provided passes the test of 11.
    * @version 2.0
    * @param $sofinummer The sofinummer to be checked.
    * @return boolean
    * @author Ivo Peters
    * @author Berry Langerak, al had Ivo hem als eerste goed :)
    **/
    function isSofinummer( $sofinummer ) {
      $snummer = trim( $sofinummer );
      // lijst met nummers die qua check kloppen, maar toch niet geldig zijn
      $aInvalid = array( '111111110',
                        '999999990',
                        '000000000' );
      if( strlen( $snummer ) != 9 || !ctype_digit( $snummer ) || in_array( $snummer, $aInvalid ) ) {
        return false;
      }
      for( $i = 9, $som = -$snummer % 10; $i > 1; $i-- ) {
        $som += $i * $snummer{( 9 - $i )};
      }
      return ( $som % 11 == 0 );
    }
  }

  if (!isSofinummer($entered_value)) {
    form_set_error($element_name_for_error, t('%entered_value is not a valid social insurance number.'));
  }
sportel’s picture

Thanks GuyPaddock, this works! Except for the %entered_value in:

form_set_error($element_name_for_error, t('%entered_value is not a valid social insurance number.'));

This only returns the string "%entered_value is not a valid social insurance number."

Thanks for your help. This also helps me a lot with making other validation rules.

Mike.

guypaddock’s picture

Sorry, that should be:

form_set_error($element_name_for_error, t('%entered_value is not a valid social insurance number.', array('%entered_value' => $entered_value));
sportel’s picture

@ GuyPaddock,

Wow, maybe I'll learn to come up with these thing myself one day (I'm a php-noob). Thanks for your help.
I like Covert Fields more and more btw.

Mike.

edit:

It still doesn't work, but I'll try to figure it out.

john.karahalis’s picture

Greetings Mike,

Were you ever able to get the validation working? If not, we'd like to work with you to (finally :-)) resolve this issue.

john.karahalis’s picture

Thank you for the interest, mmaaria.

I've changed the title back to "Social insurance number validation". Please be aware that the field "Issue title" is not a subject line, it is the name of this topic. (Don't worry, many people make this mistake.) Thanks!

As for the support request, we are still willing to help sportel resolve his original issue, but have not heard from him yet.

edit: It looks like mmaaria's comment was deleted, possibly due to spam.

john.karahalis’s picture

Thanks tomleen. Will do.

ಠ_ಠ

edit: tomleen's comment (previously #8) has been removed due to spam.

sportel’s picture

Hi Open Publishing,

Sorry for this very late reply. I have been busy with other things.
The validation works perfectly. The only thing not working is:

form_set_error($element_name_for_error, t('%entered_value is not a valid social insurance number.', array('%entered_value' => $entered_value));

This only returns: "'%entered_value is not a valid social insurance number."
I have solved this for now by using "This is not a valid blablabla..." as an error message.

Mike.

john.karahalis’s picture

Hello again, sportel.

I noticed two problems with your code snipped:

  1. The code you provide in the "Code for Validation" field should not contain PHP delimeters (<?php and ?>). I am guessing those are included above only because Drupal requires them in these posts, and that the delimiters are not present in your actual Code for Validation. Is that correct?
  2. On line 2, you should have one more closing parenthesis before the semicolon. Covert Fields should have caught this syntax error. If it did not, please let us know and we will try to determine why it did not.

Try correcting these two issues. If that does not fix the problem, make sure your code does not contain any typos. The following worked correctly for me, replacing %entered_value with the text I entered into the Covert Field:

form_set_error($element_name_for_error, t('%entered_value is not a valid social insurance number.', array('%entered_value' => $entered_value)));
sportel’s picture

Hi john.karahalis,

Yes, it was the parenthesis that was missing. I should have seen that.
I used the php delimeters btw for syntaxhighlighting here in the post.

Covert Fields did detect the syntax error, so my comment in #10 wasn't correct (it seems i didn't remembered the problem correctly when i wrote it).
Thanks for your help and your patience. And again: Great module!!!

Mike.