简体   繁体   English

使用Judge gem验证Rails 4中的电子邮件字段。 跨浏览器问题

[英]Validate e-mail field in Rails 4 with Judge gem. Cross browser problems

I have simple registration form. 我有简单的注册表。 I have set up multiple validations. 我已经设置了多个验证。 All of them work except email format validation. 除电子邮件格式验证外,所有其他功能均有效。

In model: 在模型中:

 validates :name,:region, :age, :height,:phone_number,:weight, :email,
            :description,
            :presence => {:message => "empty value found"}

validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i

View: 视图:

<%= f.email_field :email,:placeholder=> "E-pasts *", :validate => true%> 

Script: 脚本:

function validate() {
    judge.validate(document.getElementById('advertisement_email'), {
      valid: function(element) {
        element.style.border = '1px solid green';
      },
      invalid: function(element, messages) {
        element.style.border = '1px solid red';
        alert(messages.join(','));
      }
    });
}

Validation is triggered from submit button. 通过提交按钮触发验证。

  <div class="clickable8 ">
              <%= link_to "PIEVIENOT", "#", :onclick => "validate()" %>
                    <span style="padding-left:132px;">PIEVIENOT</span>
   </div>

Problems: 问题:

1) In Chrome validation passes even if nothing is in email field. 1)即使在电子邮件字段中没有任何内容,Chrome浏览器中的验证也会通过。

2) In Firefox validation works in the same moment when I am writting in that field. 2)在Firefox中,验证工作与我在该字段中撰写内容的同时进行。

It passes with valid e-mail and fails with invalid e-mail. 它以有效的电子邮件传递,而以无效的电子邮件失败。 As I need. 根据我的需要。

Question: How can I trigger validation with that click button in both browsers ? 问题: 如何在两个浏览器中使用该单击按钮来触发验证?

Thanks in advance. 提前致谢。

Check your Javascript console for errors. 检查您的Javascript控制台是否有错误。 Rails regex are not always compatible with Javascript regex, and it can change among browsers. Rails regex并不总是与Javascript regex兼容,并且可以在浏览器之间进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM