1

我正在尝试使用http://keith-wood.name/realPerson.html jquery 插件创建验证码指令。

我对 angularjs 比较陌生,似乎找不到办法做到这一点。基本上我想要一个验证码来验证一个人正在注册他们的帐户。调用 element.realperson() 将生成一个隐藏的输入字段,其中包含一些我需要与服务器端输入的输入进行比较的散列值。

因此,如果我调用这个非常基本且不完整的指令,它会将 newUser.captchaInput 模型绑定到输入表单,但我一生都无法弄清楚如何获取隐藏字段值 $('#captcha_hash') .val() 并以某种方式将其包含在表单数据中。理想情况下为 newUser.captchaHash。

angular.module('vah').directive("captcha", ->
  restrict: "A"
  require: '?ngModel'
  link: (scope, element, attrs, ngModel) ->
    return if !ngModel

    optionsObj = {
      length: 5
    }

  element.realperson(optionsObj)

  # need to bind $('#captcha_hash').val() to a newUser.captchaHash model, or 
  add the model to that generated input field.

)

<input captcha id="defaultReal" ng-model="newUser.captchaInput">

我确信有一个简单的解决方案,并希望得到任何帮助。

4

2 回答 2

1

如果你喜欢 reCaptcha,你可以尝试使用VividCortex/angular-recaptcha

于 2013-06-13T18:38:30.687 回答
0

Working code, thanks to some help from someone on a different programming forum. It's definitely not ideal, and I have a lot of learning to do, but this works.

angular.module('vah').directive("captcha", ($timeout, $parse) ->
  restrict: "A"
  require: '?ngModel'
  link: (scope, element, attrs, ngModel) ->
    return if !ngModel

    optionsObj = {
      length: 5
    }

    $timeout(->
      attrs.foo = $('#captcha_hash')
      hashSet = $parse(attrs.ngModel).assign
      scope.$watch(attrs.foo.val(), (newVal) ->
        hashSet(scope, newVal)
      )
    , 300)
    element.realperson(optionsObj)

)

Also, I'll include the ruby code for hashing of this particular captcha, since I ran into problems with Bignum and ruby compared to the way it's done in javascript, or their PHP/Java examples.

module CaptchaHashing
  module ClassMethods
    def rp_hash(value)
      hash_value = 5381
      value = value.upcase
      value.chars.each do |c|
        hash_value = ((left_shift_32(hash_value, 5)) + hash_value) + c.ord
        puts hash_value
      end
      hash_value
    end

    def left_shift_32 x, shift_amount
      shift_amount &= 0x1F
      x <<= shift_amount
      x &= 0xFFFFFFFF 

      if (x & (1<<31)).zero?
       x
      else
       x - 2**32
      end
    end

  end

  def self.included(receiver)
    receiver.extend ClassMethods
  end
end

and

def self.valid_captcha?(captcha_hash, captcha_input)
    if captcha_hash.present? && captcha_input.present?
      rp_hash(captcha_input) == captcha_hash
    else
      false
    end
  end

Good luck!

于 2013-05-11T13:57:35.170 回答