简体   繁体   English

jQuery:如何成功完成验证?

[英]Jquery: How to validation is successfully done?

I am creating a simple form that contains text fields. 我正在创建一个包含文本字段的简单表单。

These text fields are saved on change events using Ajax. 这些文本字段使用Ajax保存在更改事件中。

My question is i want to validate(jquery) these fields before change event. 我的问题是我想在更改事件之前验证(jQuery)这些字段。

Please, help. 请帮忙。 Thanks....... :) 谢谢....... :)

$('input').change(function(){
  vat retrn = validatethisfield();
if(retrn == false)
{
//show msg;
return false;
}

})

In this validate this field function , do your validation and return false if invalid data... 在此验证此字段函数中,进行验证,如果无效数据则返回false。

模糊对焦进行验证

Or on your submit button , use validation function. 或者在您的提交按钮上,使用验证功能。

<input type="submit" value= "submit it" onclick= "javascript:return formvalidator()">

always return true unless a text box is empty or something of that nature 除非文本框为空或具有此类性质,否则始终返回true

function formvalidator(){
var test = true;

if($trim.$("#myTextField").val()) == " "){
alert('Empty!!');
test = false;
}

return test;
}

There are many ways of doing this, thou plugins are readily available,for start you can learn you can write your code which is very easy to learn one of link you can refer is this one.... 这样做的方法有很多,您可以轻松使用插件,一开始您可以学习可以编写代码,这很容易学习,您可以参考的链接之一就是这个。

It covers almost all aspect of form validation: 它涵盖了表单验证的几乎所有方面:

link 链接

jQuery.ajax has a beforeSend. jQuery.ajax具有一个beforeSend。 You can do your validations in this function. 您可以在此功能中进行验证。

$.ajax({
  url: '/some/path',
  beforeSend: function( xhr ) {
    // do your validations here
  },
  success: function( data ) {
    // handle success
  }
});

To quote jquery's documentation : 引用jquery的文档

beforeSend(jqXHR, settings) - A pre-request callback function that can be used to modify the jqXHR (in jQuery 1.4.x, XMLHTTPRequest) object before it is sent. beforeSend(jqXHR,settings)-请求前回调函数,可用于在发送jqXHR对象(在jQuery 1.4.x中,XMLHTTPRequest)之前对其进行修改。 Use this to set custom headers, etc. The jqXHR and settings maps are passed as arguments. 使用它来设置自定义标头等。jqXHR和设置映射作为参数传递。 This is an Ajax Event. 这是一个Ajax事件。 Returning false in the beforeSend function will cancel the request. 在beforeSend函数中返回false将取消请求。 As of jQuery 1.5, the beforeSend option will be called regardless of the type of request. 从jQuery 1.5开始,无论请求的类型如何,都会调用beforeSend选项。

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

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