jQuery以验证表单中的序列号

时间:2018-04-25 17:30:15

标签: javascript jquery forms

我在WordPress中使用表单插件,允许JavaScript查询。

我被告知使用这样的东西:jQuery("#{{elemnt_id}}").val()

在表单插件中有一个部分用于在提交之前输入代码,如下所示:

// 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.
}

我需要根据一些小数学方程来验证序列号。 序列号的格式为:abcd-efghij(以数字的形式,但我在这里使用格式的字母,以便我可以更容易地解释会发生什么

所以如果符号序列号有效:

  1. a始终是数字1或2
  2. bcd在步骤11中进一步生成
  3. 然后将
  4. e乘以1
  5. 然后将
  6. f乘以2
  7. 然后将
  8. g乘以3
  9. 然后
  10. h乘以4
  11. 然后乘以5
  12. 然后
  13. j乘以6
  14. 然后将
  15. efghij加在一起并乘以3
  16. a乘以11并加到上一步的结果中(efghij的总数乘以3)
  17. 然后将
  18. 3添加到其结果中,然后结果等于bcd应该
  19. 的结果

    因此,有效数字将类似于1287-123456,因为

    从第二组数字开始:

    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
    

    结果全部增加= 91(1 + 4 + 9 + 16 + 25 + 36)

    然后乘以3: - 91x3 = 273

    然后从第一组数字开始:

    第一位乘以11: - 1x11 = 11

    然后将第二组的结果添加到第一组的结果:

    273 + 11 = 284
    

    最后加3:

    284 + 3 = 287
    

    给你第二个第三和第四位

    我试过这个,但可能完全错了..

    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
    

    有任何帮助,请在表单插件部分中使用正确的代码吗?

2 个答案:

答案 0 :(得分:1)

我不熟悉WordPress,但是:说你有像

这样的输入
<input type="text" id="serial"/>

然后你确实可以使用jquery检索值:

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

然后你必须在val上做你的逻辑。但是,看起来您发布的代码是可视化基本代码?您是否在javascript中寻找所描述的实现?

我会从

开始
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;
等等......剩下的应该很容易在这里找到

答案 1 :(得分:-1)

您可以在提交表单之前添加jQuery列表器(请参阅this answer或jQuery.submit docs),然后从文本字段中拆分字符并完成您的魔法。 Here是一个小提琴:https://jsfiddle.net/strauman/qef7rsxc/

因此,您的before_submit功能变为

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;
})