简体   繁体   English

在提交Ajax表单之前进行验证

[英]validation before ajax form submission

How do i introduce validation before remote_form_for submits? 如何在remote_form_for提交之前引入验证?

I have javascript function called validateForm() . 我有一个称为validateForm()的 javascript函数。 I am able to call it before the AJAX request process. 我可以在AJAX请求流程之前调用它。 I tried to use return false and event.preventDefault . 我试图使用return falseevent.preventDefault But there seems to be no effect. 但是似乎没有效果。 Here is what my code looks like 这是我的代码的样子

<% remote_form_for :customer, :update =>"uxScreenLoaderDiv", :url => {:action => "login", :controller => "site"},  :onsubmit => "validateForm(event)" do |f| %>
    User name : <%= f.text_field "uxUserName", :class => "TextBox", :style => "width:100px;" %>&nbsp;*
    &nbsp;Password : <%= f.password_field "uxPassword", :class => "TextBox", :style => "width:100px;" %>&nbsp;*
    <%= f.submit "Go!", :class => "Button-Simple", :id => "uxSubmitButton" %>   
<% end %>

the javascript function is simple as follows javascript函数很简单,如下所示

function validateForm(event){
   return false;
   //event.preventDefault();
}

Try this 尝试这个

<% remote_form_for :customer, :update =>"uxScreenLoaderDiv", :url => {:action => "login", :controller => "site"}, :html => {:id => "uxLoginForm"}, :onsubmit => "return validateForm(event)" do |f| %>

ie change 即改变

 :onsubmit => "validateForm(event)

to

 :onsubmit => "return validateForm(event)

EDITED it should be 编辑它应该是

<% remote_form_for :customer, :update =>"uxScreenLoaderDiv", 
         :url => {:action => "login", :controller => "site"}, 
         :html => {:id => "uxLoginForm", :onsubmit => "return validateForm(event)"} 
         do |f|
 %>

EDITED AGAIN 再次编辑

WHY don't you use :condition for it? 为什么不使用:condition? something like following 像下面这样

<% remote_form_for :customer, :update =>"uxScreenLoaderDiv", 
         :url => {:action => "login", :controller => "site"}, 
         :html => {:id => "uxLoginForm"},
         :condition=>"validateForm(event)!=false"
         do |f|
 %>

I think what you're looking for is something like onSubmit="return validateForm(this)" or thereabouts. 我认为您正在寻找的是类似onSubmit =“ return validateForm(this)”之类的东西。 Then if the validation returns false (because it failed validation) the form should not submit. 然后,如果验证返回false(因为验证失败),则不应提交表单。

You might want to check LIvevalidation plugin ( http://github.com/augustl/live-validations ) which you can you to do Ajax with active record validations 您可能需要检查LIvevalidation插件( http://github.com/augustl/live-validations ),可以使用活动记录验证来执行Ajax

And its always good to have non-ajax validations also (even though you use ajax validations) as ajax will not work in javascript disable browser 同时也具有非ajax验证(即使您使用ajax验证)也总是很好,因为ajax在javascript禁用浏览器中不起作用

cheers, sameera 欢呼声,sameera

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

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