JQuery Ajax submission Form and form validation
Blog Category:
- Jsp/html/javascript/ajax/development Kit
- Open Source Projects
Note: It is verified that the formvalidator is only suitable for a single page with no more than one form.
This example implements the ability to submit a form via Ajax and validate the form's data before submitting it.
Mainly through two plug-ins to achieve:
The first one:
JQuery Form Plugin
Http://jquery.malsup.com/form main function is the AJAX submission form
The second: JQuery formvalidator domestic powerful form validation plugin.
Http://www.cnblogs.com/wzmaodong
The simplest examples are as follows:
HTML code
- <html>
- <head>
- <title> Form</title>
- <!--introducing jquery--
- <script src="<?php Echo base_url () >style/jquery.js" type="Text/javascript"></ script>
- <!--Introducing jquery Formvalidator---
- <link type= "Text/css" rel= "stylesheet" < Span style= "color: #ff0000;" >href= "<?php echo base_url ()?> Style/form_valide/style/validator.css " > </link>
- <script src= type= "Text/javascript" charset= "UTF-8" ></script>
- <script src="<?php echo base_url ()? >style/form_valide/formvalidatorregex.js" type="Text/javascript " charset=" UTF-8 "></script>
- <!--Introducing jquery Form Plugin---
- <script type="Text/javascript" src= "<?php echo base_url ()? >style/ajax_form/jquery.form.js" ></script>
- <script type="Text/javascript">
- $ (document). Ready (function () {
- Provides a callback function that provides handling of the result of a commit return
- var options = {
- Success:function (data) {
- alert (data);
- }};
- Initializes the validation plug-in, and if the commit error occurs, the dialog box pops up and, if correct, submits the form request via Ajax.
- $.formvalidator.initconfig ({
- Formid: "Form1",
- Onerror:function (msg) {alert (msg)},
- Onsuccess:function () {$ (' #form1 '). Ajaxsubmit (options); return false;}
- });
- Form validation
- $ ("#test1"). Formvalidator ({
- OnShow: "Please enter the user name, only input \" maodong\ "is Right",
- onfocus: "User name at least 6 characters, maximum 10 characters",
- Oncorrect: "The user name can be registered"})
- . Inputvalidator ({min:6,max:10,onerror: "The user name you entered is illegal, please confirm"})
- . Regexvalidator ({regexp: "username", datatype: "Enum", OnError: "Incorrect user name format"});
- });
- </Script>
- </head>
- <body>
- <form action= " php echo site_url ("welcome/post") ?> " < Span style= "color: #ff0000;" >method= "post" name= "Form1" id= "Form1" >
- <TD align="right"> user name:</td>
- <TD><input type= "Text" name= "test1" < Span style= "color: #0000ff;" >id= "Test1" style=< Span style= "color: #0000ff;" > "width:120px" value= />< span class= "tag" ></td>
- <TD><div id="Test1tip" style="width:250px"></div></td >
- <input type="Submit" name="button" id="button" value="Submit" />
- </form>
- </Body>
- </html>
The success callback function is not executed and may be a request-time error. Try to parse the data returned by the server into datatype format before calling the success method, and try Error:function (xmlresponse) {alert (Xmlresponse.responsetext)}.
Such as:
var options={
Target: ' #output1 ',//Put the server back into an element with ID OUTPUT1
Beforesubmit:showrequest,//Pre-commit callback function
Success:showresponse,//post-commit callback function
Error:function (xmlresponse) {alert (Xmlresponse.responsetext)},
Url:url,//default is the action of form, if stated, Overrides
Type:type,//default is the method of form (' get ' or ' post '), if stated, will overwrite
Datetype:null,//' xml ', ' script ', or ' JSON ' (type returned by the receiving server)
Clearform:true,//After successful commit, clears the values of all form elements
Resetform:true,//After successful commit, reset the values of all forms
timeout:3000//Limit the request time, when the request is greater than 3 seconds, jump out of the request
}
options for Ajaxform and Ajaxsubmit
In addition to the options listed below, you can also pass the standard $.ajax option to both functions.
Target ———— which elements in the specified page are used to respond to server-side updates. This value can be a jquery query string, or it specifies a jquery object, or a DOM element. Default value: null
Replacetarget ———— with the target option. When set to True, the specified portion of the target is replaced, or only its contents are replaced. Default value: False
The URL ———— specifies the URL to which the data is to be submitted. Default value: The Action property value of the form
Type ———— specify how the form is submitted: ' GET ' or ' POST '. Default value: The Method property value of the form (if none is ' GET ')
Data ———— The additional information to be submitted in the form of a singleton, format: data: {key1: ' value1′, Key2: ' value2′}
DataType ———— The data type of the server response, the value can be: null, ' xml ', ' script ', or ' json '. This option specifies how the server-side response is handled. It maps directly to the Jquery.httpdata method. If datatype is ' XML ', the server's response will be treated as XML, and the ' success ' callback function will be passed to the value of Responsexml. If datatype is ' JSON ', the server's response will be treated as a JSON object (via eval) and passed to the ' success ' callback function. If datatype is ' script ', the server's response will be eval into a global context. Default value: null
BeforeSerialize ———— The callback function before the form is serialized. It provides an opportunity to control the form before the value is fetched. One of the parameters is the form's jquery object, and the other is the option object passed to Ajaxform/ajaxsubmit. Default value: null
Beforeserialize:function ($form, Options) {
Returns false to cancel the commit operation
}
Beforesubmit ———— The callback function before the form is submitted. Typically used to process logic before a form is submitted, such as validating form data. If this callback function returns FALSE, the commit operation will be canceled. Its three parameters are an array of form data, a jquery object for the form, and an option object passed to Ajaxform/ajaxsubmit. Default value: null
Beforesubmit:function (arr, $form, options) {
The format of the first parameter:
[{name: ' username ', value: ' Jresig '}, {name: ' Password ', value: ' Secret '}]
Returns false to cancel the commit operation
}
Success a callback function when the server side generates a response ———— the form is successfully committed. It will be passed the following parameters:
1. Value of ResponseText or Responsexml (depending on the value of the datatype option)
2. StatusText
3. XHR (XMLHttpRequest object) (jquery encapsulated FORM element if the version of jquery is less than 1.4)
4. jquery encapsulated form elements (undefined if the version of jquery is less than 1.4)
Default value: null
Semantic ————— Specifies whether the submitted data follows the semantic order (slower than normal). Note In general, the serialization of a form follows the semantic order, unless there is a control of the image type. Set to True only if the server has strict requirements on the semantics of the submitted data and the form contains a control of the image type. Default value: False
Resetform ———— whether to reset the form when it is successfully submitted. Default value: null
ClearForm ———— whether to clear the form when it is successfully submitted. Default value: null
Whether the IFRAME ———— always put the server's response inside the IFRAME. Default value: False
IFRAMESRC ———— If the IFRAME option is true, the SRC attribute to be added to the IFRAME. Default value: About:blank, the default value for pages that use the HTTPS protocol is: Javascript:false
Forcesync ———— Specifies whether to remove short delays when uploading a file or submitting a form using the IFRAME option. This delay is used by the browser to render the DOM before the original commit operation. This improves usability by giving users hints such as "Please Wait". Default value: False
Examples of options:
Prepare option Object
var options = {
Target: ' #divToUpdate ',
URL: ' comment.php ',
Success:function () {
Alert (' Thanks for your comment! ');
}
};
Passing the option object to the Ajaxform function
$ (' #myForm '). Ajaxform (options);
JQuery Ajax submission Form and form validation