Custom PHP script code to validate Swedish SSN number.

  • Profile Image
    Asked on January 18, 2015 at 06:39 PM

    Hello everyone!

    Does anyone know if it is possible to add a "validating function" to a specific field i created? I'm creating a order form where I need to validate the customers social security number but a Swedish one.

    I made a field with input mask ######-#### ten digits and need this field to be validated.

    Merci- Tack - Thank you for helping!!!

     $pnr get_magic_quotes_gpc() ? $_POST['personnr'] : mysql_real_escape_string($_POST['personnr']);
    if (checkPnr($pnr)) {
       echo 'RÄTT!';
    } else {
       echo 'FEL!';
    function checkPnr($pnr) {
        if ( !preg_match("/^\d{6}\-\d{4}$/"$pnr) ) {
            return false;
        $pnr str_replace("-"""$pnr);
        $n 2;
        for ($i=0$i<9$i++) {
            $tmp $pnr[$i] * $n;
            ($tmp 9) ? $sum += + ($tmp 10) : $sum += $tmp; ($n == 2) ? $n $n 2;
        return !( ($sum $pnr[9]) % 10);


  • Profile Image
    Answered on January 18, 2015 at 10:57 PM


    I am not sure if the PHP script you have added above is valid to check the correct format of Swedish SSN number.

    Please be noted that it is anyway not possible to add any custom script code in form editor. You will have to download your forms' full source code and then add the required script to do the validation.

    For your requirement, I will suggest you to add custom JavaScript code to do the validation in your form's full source code. You may like to check the following URL for a sample JavaScript code: 

    Hope this helps.

    Do get back to us if you want us to further help you on adding custom JavaScript code and we will surely help you.

    Thank you!