Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

problem with js

  • RitaShen
  • RitaShen's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
3 years 8 months ago - 3 years 8 months ago #203304 by RitaShen
problem with js was created by RitaShen
Hi there,

I have a problem when I wrote the javascript to verification a set of numbers.
Briefly explain, I want to use the [short free text] question type,
and people will enter their specific number, the number will consist of one letter and 9 numbers,
also, this set of number has its own rule, if you enter the wrong numbers, will pop-up alert or cannot keep answering the survey.

here is the code I use, but it seems cannot work in the survey,
I have no idea where I went wrong.

I’m not familiar with using this, could someone please lend me a hand?
many thanks

Code:
<script type="text/javascript" charset="utf-8">
$(document).ready function CheckInput() {
   if ( ! checkID( document.Form1.ID.value ) )
      window.alert( "The ID is wrong" );
}
function checkID( id ) {
  tab = "ABCDEFGHJKLMNPQRSTUVXYWZIO"                     
  A1 = new Array (1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3 );
  A2 = new Array (0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5 );
  Mx = new Array (9,8,7,6,5,4,3,2,1,1);
 
  if ( id.length != 10 ) return false;
  i = tab.indexOf( id.charAt(0) );
  if ( i == -1 ) return false;
  sum = A1[i] + A2[i]*9;
 
  for ( i=1; i<10; i++ ) {
    v = parseInt( id.charAt(i) );
    if ( isNaN(v) ) return false;
    sum = sum + v * Mx[i];
  }
  if ( sum % 10 != 0 ) return false;
  return true;
}
</script>
Last edit: 3 years 8 months ago by RitaShen.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 8 months ago #203316 by tpartner
Replied by tpartner on topic problem with js
The script is full of errors.

Maybe you could explain more about what you are trying to do and provide a sample survey.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: DenisChenu
The topic has been locked.
  • RitaShen
  • RitaShen's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
3 years 8 months ago #203327 by RitaShen
Replied by RitaShen on topic problem with js
what I want to do is like this page: Check ID

and sorry this is the Chinese version

can we do like this page in our limesurvey?
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 8 months ago #203345 by Joffm
Replied by Joffm on topic problem with js
Well, the function "CheckID" works fine, but the function "CheckInput" has to be revised.
Now you use an HTML form with an "onClick"-event. (I wonder why there is a "document.ready")


By the way:
As I am not able to create any javascript function (only adapt some), I tried to validate your ID.
And it works.
Knowing that you can precalculate A1(i) + A2(i)*9 to A3(i):
(01,10,19,28,37,46,55,64,73,82,02,11,20,29,38,47,56,65,74,83,03,12,21,30,39,48 ),
you get this question validation equation.

(strlen(self)==10) and
(strpos('#ABCDEFGHJKLMNPQRSTUVXYWZIO',substr(self,0,1))>0) and
(is_numeric(substr(self,1))) and

(floor(sum(substr("0110192837465564738202112029384756657483031221303948", 2*strpos('ABCDEFGHJKLMNPQRSTUVXYWZIO',substr(self,0,1)),2), sum(8*substr(self,1,1), 7*substr(self,2,1), 6*substr(self,3,1), 5*substr(self,4,1), 4*substr(self,5,1), 3*substr(self,6,1), 2*substr(self,7,1), 1*substr(self,8,1), 1*substr(self,9,1)))/10)== sum(substr("0110192837465564738202112029384756657483031221303948", 2*strpos('ABCDEFGHJKLMNPQRSTUVXYWZIO',substr(self,0,1)),2), sum(8*substr(self,1,1), 7*substr(self,2,1), 6*substr(self,3,1), 5*substr(self,4,1), 4*substr(self,5,1), 3*substr(self,6,1), 2*substr(self,7,1), 1*substr(self,8,1),1*substr(self,9,1)))/10)

Well, of course it is not more than an example to show what is possible.
You could replace the first (green) lines by a Regular expression like
regexMatch('/^[ABCDEFGHJKLMNPQRSTUVXYWZIO]{1}[0-9]{9}$/',self)

Anyway: A javascript solution might be better.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 8 months ago - 3 years 8 months ago #203371 by Joffm
Replied by Joffm on topic problem with js
Hi, Rita,
I have one general question.
Mx is defined as:
Mx = new Array (9,8,7,6,5,4,3,2,1,1);

Then we have this loop
Code:
for ( i=1; i<10; i++ ) {
...
    sum = sum + v * Mx[i;
  }

The array is zero-based, the loop starts at 1.
So the leading "9" is never used. But there are two "1" at the end.

Is this really correct?

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 3 years 8 months ago by Joffm.
The topic has been locked.
  • RitaShen
  • RitaShen's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
3 years 8 months ago #203377 by RitaShen
Replied by RitaShen on topic problem with js

The array is zero-based, the loop starts at 1.
So the leading "9" is never used. But there are two "1" at the end.

Is this really correct?

Joffm


yes
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose