如何将jQuery Validate与我的aws lambda函数集成?

时间:2017-05-28 07:21:38

标签: jquery validation jquery-validate aws-lambda

我想验证我在浏览器中运行的aws lambda函数。我想使用jQuery validate插件来验证在网页上提交的输入数据。但是,我无法让它发挥作用。

这是我的lambda函数:

$('#contact-form-button').on('click', function (e) {
  AWS.config.update({ region: 'us-east-1' });
  AWS.config.credentials = new AWS.Credentials('<aws credential>', '<aws credential>');
  var lambda = new AWS.Lambda({ region: 'us-east-1', apiVersion: '2015-03-31' }),
    contactName = $('#contact_name'),
    contactEmail = $('#contact_email'),
    contactPhone = $('#contact_phone'),
    contactSubject = $('#contact_subject'),
    contactMessage = $('#contact_message');

  /*Feedback Form DynamoDB Table Data*/
  var pullParams = {
    FunctionName: 'grouve-marketing-web-dev-createFeedback',
    InvocationType: 'RequestResponse',
    LogType: 'Tail',
    Payload: JSON.stringify({
      "feedback_id": makeid(),
      "name": $(contactName).val(),
      "email": $(contactEmail).val(),
      "phone": $(contactPhone).val(),
      "subject": $(contactSubject).val(),
      "message": $(contactMessage).val()
    })
  };


  lambda.invoke(pullParams, function (error, data) {
    if (error) {
      prompt(error);
    } else {
      pullEmailResults = JSON.parse(data.Payload);
    }
  });
  var pullEmailResults;

  return false;
});

这是我的jquery验证码

 $('#contact_form').validate({
        rules: {
            "contact_email": {
                required: true,
                email: true
            },
            "contact_phone": {
                required: true,
                phoneUS: true
            }
        },
        messages: {
             "contact_name": {
                required: "nter your name"
            },
            "contact_email": {
                required: "Enter a valid email"
            },
            "contact_phone": {
                required: "Enter a valid phone number"
            },
            "contact_subject": {
                required: "Enter a subject"
            },
            "contact_message": {
                required: "Enter your message"
            }
        }      
    });

我尝试使用success:submitHandler方法,但都不起作用。我无法在click事件之外运行验证。那我该怎么办?

1 个答案:

答案 0 :(得分:1)

所以这就是我能够真正解决问题的方法!!!

&#13;
&#13;
$(document).ready(function() {
  $('#event-start-month').change(function(e) {
    console.log("Start month selected");
    if ($('#event-start-month').val() < 05) {
      $('#event-end-year option[value="2017"]').remove();
    } else if ($('#event-start-month').val() > 05) {
      $('#event-end-year option:first').after('<option value="2017">2017</option>');
    }
  });
  $('#contact_form').validate({
    rules: {
      "contact_email": {
        required: true,
        email: true
      },
      "contact_phone": {
        required: true,
        phoneUS: true
      }
    },
    messages: {
      "contact_name": {
        required: "nter your name"
      },
      "contact_email": {
        required: "Enter a valid email"
      },
      "contact_phone": {
        required: "Enter a valid phone number"
      },
      "contact_subject": {
        required: "Enter a subject"
      },
      "contact_message": {
        required: "Enter your message"
      }
    }
  });

  $('#contact-form-button').on('click', function(e) {
    if ($('#contact_form').valid()) {
      AWS.config.update({
        region: 'us-east-1'
      });
      AWS.config.credentials = new AWS.Credentials('<aws credential>', '<aws credential>');
      var lambda = new AWS.Lambda({
          region: 'us-east-1',
          apiVersion: '2015-03-31'
        }),
        contactName = $('#contact_name'),
        contactEmail = $('#contact_email'),
        contactPhone = $('#contact_phone'),
        contactSubject = $('#contact_subject'),
        contactMessage = $('#contact_message');

      /*Feedback Form DynamoDB Table Data*/
      var pullParams = {
        FunctionName: 'grouve-marketing-web-dev-createFeedback',
        InvocationType: 'RequestResponse',
        LogType: 'Tail',
        Payload: JSON.stringify({
          "feedback_id": makeid(),
          "name": $(contactName).val(),
          "email": $(contactEmail).val(),
          "phone": $(contactPhone).val(),
          "subject": $(contactSubject).val(),
          "message": $(contactMessage).val()
        })
      };


      lambda.invoke(pullParams, function(error, data) {
        if (error) {
          prompt(error);
        } else {
          pullEmailResults = JSON.parse(data.Payload);
        }
      });
      var pullEmailResults;
    }
    return false;
  });
});
&#13;
&#13;
&#13;

只需使用if if ($('#contact_form').valid()) {}就像魅力一样!!!