Rails submit_tag表单验证

时间:2015-05-22 16:30:41

标签: ruby-on-rails coffeescript

在我看来,我有:

event.html.erb

function mjpeg2jpg_convert(){
$camurl="http://admin:secretpass@192.168.1.60/Streaming/Channels/1/httppreview";

$boundary="\n--";

$f = fopen($camurl,"r") ;


   if(!$f)
   {
        //**** cannot open
        echo "error";
   }else{
    $cnt = 0;    
    //**** URL OK
    $r = "";
    $l = "";
         while (substr_count($r,"Content-Length") != 2 && $cnt<10000){
            $l.=fread($f,512);
            echo $l;
            $r.=$l;
            $cnt++;
         }

         $start = strpos($r,'ÿ');
         $end   = strpos($r,$boundary,$start)-1;
         $frame = substr("$r",$start,$end - $start);
echo "FRAME";
         //header("Content-type: image/jpeg");
         echo $frame;
   }
fclose($f);
}

......我的咖啡是:

events.js.coffee

<div class="form-group registration_form">
            <%= form_tag(register_learner_path, {remote: true}) do -%>
              <p>
              <%= label_tag :email %>
              <%= text_field_tag :email %>
              <p/>
              <p>
              <%= label_tag "First Name" %>
              <%= text_field_tag :first_name %>
              <p/>
              <p>
              <%= label_tag "Last Name" %>
              <%= text_field_tag :last_name %>
              <p/>
              <%= hidden_field_tag 'event_id', @event.id %>
              <p class="submit_register">
              <%= submit_tag "Register for this event", id: 'register_button', :onclick => "return validateform();" %>
                  <span class="help-block">Enter your name and email above and the link will be magically revealed</span>
              <p/>
            <%- end -%>
          </div>

基本上我想在这里完成两个验证。一个我想确保电子邮件,名字和姓氏中没有空字符串。其次,我想验证一个有效的&#39;基于js中的正则表达式的电子邮件。

我在这段代码中看到的行为是两个验证都在运行,但是我只希望在其他验证(空字符串测试)通过时运行电子邮件验证。

1 个答案:

答案 0 :(得分:2)

所以这感觉有点忙,但它确实有效。随意提出建议......

jQuery ->
    $group = $('.form-group.registration_form')
    if $group.length
        $(".location").hide()
        $(".location-section-link").hide()
        $(".submit_register").click ->
            if validateForm() == false
                return false
            else if validateEmail($("#email").val()) == false
                alert('Please enter a valid email address')
                return false
            else
                $(".location").show()
                $(".location-section-link").show()
                $(".registration_form").hide()

    #validate email
    validateEmail = (email) ->
      re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
      re.test email

    #validate email, first name and last name
    validateForm = ->
        if $("#email").val() == '' || $("#first_name").val() == '' || $("#last_name").val() == ''
                alert('Please enter first name, last name, and email')
                return false