0

I am using a forms plugin in WordPress that allows for JavaScript queries.

I was told to use something like this: jQuery("#{{elemnt_id}}").val()

In the forms plugin there is a section to input code before submit which is as follows:

// Occurs just before submitting  the form
function before_submit() {

 // IMPORTANT! If you want to interrupt (stop) the submitting of the form, 
this function should return true. You don't need to return any value if you
don't want to stop the submission.
}

I need to validate a serial number based on some minor mathematical equations. The serial number is in the format of: abcd-efghij (in the form of numbers but I am using letters in the format here so that I can explain easier what happens

So the serial number is valid if:

  1. a is always either the numbers 1 or 2
  2. bcd is generated further along in step 11
  3. e is then multiplied by 1
  4. f is then multiplied by 2
  5. g is then multiplied by 3
  6. h is then multiplied by 4
  7. i is then multiplied by 5
  8. j is then multiplied by 6
  9. efghij is then all added up together and multiplied by 3
  10. a is multiplied by 11 and added to the result of previous step (total of efghij muliplied by 3)
  11. 3 is then added to the result of that and the result then equals what bcd should be

So a valid number would be something like 1287-123456 because

From second set of digits:

5th digit multiplied by 1:-   1x1=1
6th digit multiplied by 2:-   2x2=4
7th digit multiplied by 3:-   3x3=9
8th digit multiplied by 4:-   4x4=16
9th digit multiplied by 5:-   5x5=25
10th digit multiplied by 6:-  6x6=36

results added all up = 91 (1+4+9+16+25+36)

then multiply by 3:- 91x3=273

Then from first set of digits:

1st digit multiplied by 11:- 1x11=11

Then add result of second set to result of first set:

273 + 11 = 284

and finally add 3 to that:

284 + 3 = 287

giving you 2nd 3rd and 4th digits

I have tried this but its probably totally wrong..

Dim strID
Dim ColCSum3
Dim ChkVal
Dim InitVal
strID = "element_id"
If strID = "" Then
        ''''' return false
        '''' Return "Invalid"
End If
If Mid(strID, 5, 1) <> "-" Or Len(strID) <> 11 Then
        '''' return false
        '''' Return "Invalid"
End If

InitVal = CLng(Left(strID, 1))
ChkVal = CLng(Mid(strID, 2, 3))

ColCSum3 = (1 * CLng(Mid(strID, 6, 1)) + 2 * CLng(Mid(strID, 7, 1)) + 3 * CLng(Mid(strID, 8, 1)) + 4 * CLng(Mid(strID, 9, 1)) + 5 * CLng(Mid(strID, 10, 1)) + 6 * CLng(Mid(strID, 11, 1))) * 3
If 11 * InitVal + ColCSum3 + 3 = ChkVal Then
Return "Validated"
Else
Return "Invalid"
End If

Any help please for the correct code to use in the form plugin section?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Alex
  • 29
  • 1
  • 9

2 Answers2

1

Im not familiar with WordPress, however: say you have an input like

<input type="text" id="serial"/>

then you can indeed retrieve the value using jquery:

var inputVal = $('#serial').val();

Then you would have to do your logic on the val. However, it looks like the code you posted is visual basic code? Are you looking for the described implementation in javascript?

I would start by

if(inputVal.indexOf('-')!=4)return false;
var seqs = inputVal.split('-');
var seq1= parseInt(seqs[0]);
var seq2= parseInt(seqs[1]);
if(isNaN(seq1) || isNaN(seq2))return false;

etc... The rest should be easy to find here and there

user1515791
  • 675
  • 4
  • 20
  • yes it is taken from VBS and I was trying to convert to JS or JQuery equivalent. Thanks for your answer, It is helpful.. I will do more research based on that – Alex Apr 25 '18 at 17:52
  • You can get the value at a specific index in a digit by using modulo and division and do everything like so (e.g. 'Math.floor((1234%1000)/100)' will give 2) Im sure there are more elegant ways, but thats enough to get the job done... – user1515791 Apr 25 '18 at 17:58
  • Thanks. I will play around with that. It may take a while lol. Thank you! – Alex Apr 25 '18 at 18:40
  • To get a value from an number at an index: function getAtIndex(nr, index){ var d = Math.pow(10, index); return Math.floor((nr%(d*10))/d); } – user1515791 Apr 26 '18 at 11:47
-1

You can add a jQuery listner before the form is submitted (see this answer or the jQuery.submit docs), and then split the characters from the text field and do your magic. Here is a fiddle: https://jsfiddle.net/strauman/qef7rsxc/.

So your before_submit function becomes

function before_submit(){
  // If you write return false; in here, then
  // the form will not get sent.
  // if you write return true; then the form will
  // get sent:
  // Get the value of the textbox
  inputVal = $("#serial").val();
  // From @user1515791 answer
  // Split it at the dash (-)
  console.log(inputVal.indexOf('-'));
  if (inputVal.indexOf('-') != 4) {
    $("#errmsg").html("Wrong format. Needs four numbers before dash");
    return false;
  }
  var seqs = inputVal.split('-');
  var seq1 = seqs[0];
  var seq2 = seqs[1];
  // seq1 now contains abcd
  // and sec2 contains efghij
  if (isNaN(parseInt(seq1)) ||isNaN(parseInt(seq2))) {
    $("#errmsg").html("Got non-numbers in the sequence");
    return false;
  }
  // You can extract the numbers to variables
  // like in your question like this
  a = seq1[0];
  b = seq1[1];
  c = seq1[2];
  d = seq1[3];
  e = seq2[0];
  f = seq2[1];
  //...
  j = seq2[5];
  if (a != 1 && a != 2) {
    $("#errmsg").html("the first number is not 1 or 2");
    return false;
  }
  f = f * 2;
  // ...
  return false;
})
  • Wow thanks! I think I get it. makes sense. The only think I am not sure of, in the plugin on WordPress, I can add javascript to the form, and in there already exists // IMPORTANT! If you want to interrupt (stop) the submitting of the form, this function should return true. so by returning false would the form not submit? – Alex Apr 25 '18 at 18:58
  • "Don’t forget to upvote and/or mark answer as accepted if it is satisfactory" you copied the code from my answer. How does yours extend mine? BTW the part you wrote (seq1[0] etc) is wrong - you cant use index on a number – user1515791 Apr 26 '18 at 11:46
  • @user1515791 - "you copied the code from my answer. ": I did actually write my own code, but then I realized that your way of parsing the integers were a bit neater, so I decided to use that and give you credits for it (in the comments of the code). - "How does yours extend mine?" since the OP says he is new to JS and JQ, I provided a more complete example, where the OP can play around with a fiddle - `seq1[0]` works fine: `seq1=Array(1,3,2); seq[0];` returns `1` as expected. Sorry if you feel like I stepped on your toes. – Andreas Storvik Strauman Apr 26 '18 at 11:54
  • Also "Don’t forget to upvote and/or mark answer as accepted if it is satisfactory" is something I write in the comments for many of my answers when the question is asked by new people. I see now that this encouragement is wrong because it might be interpreted as favoring only _my_ answer, which is not the intention. – Andreas Storvik Strauman Apr 26 '18 at 11:57
  • seq1=Array(1,3,2); seq[0]; returns 1 as expected - sure, but in the code, seq1 isn't an array; its an int. Itll return undefined. – user1515791 Apr 26 '18 at 13:05
  • Ok, me too (forgot the '1') however, seq1 is still an int :) – user1515791 Apr 26 '18 at 13:09
  • Oh. I see. You're completely right! I thought you did something different than what you're doing. – Andreas Storvik Strauman Apr 26 '18 at 13:10
  • Thanks guys. I am still trying to figure it all out. Both helpful and I really appreciate it. After I do all the multiplications for the sequence of numbers like f = f * 2; etc, how do I add them up and multiply result by 3? – Alex Apr 27 '18 at 05:51