#BlackLivesMatter
Do you build things with AMP? Fill out the new AMP Developer Survey!
AMP
  • websites

amp-bind-recaptcha

Introduction

This example shows how to create recaptcha input using just amp-bind.

Import amp-bind so recaptcha can have multiple states

<script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>

Recaptcha input used to verify user for amp-form

<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>

Implementation

The amp-state below defines three different states that the recaptcha equation could be.

<amp-state id="captcha">
  <script type="application/json">
    {
      "state1": {
        "result": "10",
        "condition": "+",
        "captchaCorrect": "6"

      },
      "state2": {
        "result": "2",
        "condition": "-",
        "captchaCorrect": "6"
      },
      "state3": {
        "result": "8",
        "condition": "*",
        "captchaCorrect": "2"
      }
    }
  </script>
</amp-state>

Form

Recaptcha forces users to provide the correct input using the [pattern] requirement. The [pattern] updates dynamically as the state changes. In order for the recaptcha to work on first pass, disable the input until the amp-bind 'state' variable is set (i.e. [disabled]="!state"). As another workaround, you could set a default pattern (without brackets) and have the [pattern] update as user refreshes the conditional. Upon refresh the 'state' is updated to provide a new equation.

+ 4 = 10
<form action="https://www.google.com/"
  method="get"
  target="_top">
  <input name="s"
    placeholder="Type Anything"
    type="text"
    on="input-debounced:AMP.setState({state: 'state1'})"
    required>
  <input [disabled]="!state"
    disabled
    type="text"
    name
    [pattern]="captcha[state].captchaCorrect"
    title="AMP recaptcha input"
    required>
  <span [text]="captcha[state].condition">+</span>
  <span>4</span>
  <span>=</span>
  <span [text]="captcha[state].result">10</span>
  <span on="tap:AMP.setState({state: (state == 'state1' ? 'state2' : state == 'state2' ? 'state3': 'state1')})"
    role="button"
    tabindex="0">
    <amp-img src="https://fonts.gstatic.com/s/i/materialicons/autorenew/v4/24px.svg"
      width="24"
      height="24"></amp-img>
    <input type="submit"
      value="Submit">
  </span></form>
자세한 설명이 필요하신가요?

이 페이지의 설명만으로 궁금한 점이 모두 해결되지 않는다면 다른 AMP 사용자에게 문의하여 구체적인 활용 사례를 논의해 보세요.

Stack Overflow로 이동
설명이 부족한 기능을 발견하셨나요?

AMP 프로젝트는 여러분의 참여와 기여를 적극 환영합니다! 오픈 소스 커뮤니티를 통해 지속적으로 활동해 주셔도 좋지만 관심 있는 주제에 한 번만 기여하셔도 큰 도움이 됩니다.

GitHub에서 샘플 수정하기