You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: page/ajax/ajax-and-forms.md
+19-13Lines changed: 19 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,22 +10,24 @@ Serializing form inputs in jQuery is extremely easy. Two methods come supported
10
10
11
11
The `serialize` method serializes a form's data into a query string. For the element's value to be serialized, it **must** have a `name` attribute. Please noted that values from inputs with a type of `checkbox` or `radio` are included only if they are checked.
12
12
13
-
14
-
<javascriptcaption="Turning form data into a query string">
13
+
```
14
+
// Turning form data into a query string
15
15
$('#myForm').serialize(); // creates a query string like this: field_1=something&field2=somethingElse
16
-
</javascript>
16
+
17
+
```
17
18
18
19
While plain old serialization is great, sometimes your application would work better if you sent over an array of objects, instead of just the query string. For that, jQuery has the `serializeArray` method. It's very similar to the `serialize` method listed above, except it produces an array of objects, instead of a string.
19
20
20
-
<javascriptcaption="Creating an array of objects containing form data">
21
+
```
22
+
// Creating an array of objects containing form data
21
23
$('#myForm').serializeArray();
22
24
23
25
// creates a structure like this:
24
26
// [
25
27
// { name : 'field_1', value : 'something' },
26
28
// { name : 'field_2', value : 'somethingElse' }
27
29
// ]
28
-
</javascript>
30
+
```
29
31
30
32
### Client-side validation
31
33
Client-side validation is, much like many other things, extremely easy using jQuery. While there are several cases developers can test for, some of the most common ones are: presence of a required input, valid usernames/emails/phone numbers/etc..., or checking an "I agree..." box.
@@ -34,7 +36,8 @@ Please note that it is advisable that you also perform server-side validation fo
34
36
35
37
With that being said, let's jump on in to some examples! First, we'll see how easy it is to check if a required field doesn't have anything in it. If it doesn't, then we'll `return false`, and prevent the form from processing.
36
38
37
-
<javascriptcaption="Using validation to check for the presence of an input">
39
+
```
40
+
// Using validation to check for the presence of an input
38
41
$("#form").submit(function( e ) {
39
42
if ( $(".required").val().length === 0 ) { // if .required's value's length is zero
40
43
// usually show some kind of error message here
@@ -44,11 +47,12 @@ $("#form").submit(function( e ) {
44
47
// run $.ajax here
45
48
}
46
49
});
47
-
</javascript>
50
+
```
48
51
49
52
Let's see how easy it is to check for invalid characters in a username:
50
53
51
-
<javascriptcaption="Validate a phone number field">
54
+
```
55
+
// Validate a phone number field
52
56
$("#form").submit(function( e ) {
53
57
var inputtedPhoneNumber = $("#phone").val()
54
58
, phoneNumberRegex = ^\d*$/; // match only numbers
@@ -61,7 +65,7 @@ $("#form").submit(function( e ) {
61
65
// run $.ajax here
62
66
}
63
67
})
64
-
</javascript>
68
+
```
65
69
66
70
67
71
@@ -70,19 +74,21 @@ A prefilter is a way to modify the ajax options before each request is sent (hen
70
74
71
75
For example, say we would like to modify all crossDomain requests through a proxy. To do so with a prefilter is quite simple:
72
76
73
-
<javascriptcaption="Using a proxy with a prefilter">
You can pass in an optional argument before the callback function that specifies which `dataTypes` you'd like the prefilter to be applied to. For example, if we want our prefilter to only apply to `JSON` and `script` requests, we'd do:
83
88
84
-
<javascriptcaption="using the optional dataTypes argument">
0 commit comments