-
Notifications
You must be signed in to change notification settings - Fork 64
/
patternRequiredWithVisibleIf.html
111 lines (91 loc) · 4.18 KB
/
patternRequiredWithVisibleIf.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link type="text/css" rel="stylesheet" href="css/form.css" />
<link type="text/css" rel="stylesheet" href="../../shared/css/visibleIf.css" />
<title>HTML5 pattern and required Attribute Test With VisibleIf</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<!-- Needed for visibleIf -->
<script type="text/javascript" src="../../shared/js/EventHelpers.js"></script>
<script type="text/javascript" src="../../shared/js/visibleIf.js"></script>
<!-- Needed for Forms Feature Detection -->
<script type="text/javascript" src="../../shared/js/modernizr.com/Modernizr-2.5.3.forms.js">
</script>
<!-- Needed for Validation -->
<script type="text/javascript" src="../../shared/js/html5Forms.js" data-webforms2-support="validation" data-webforms2-force-js-validation="true">
</script>
</head>
<body id="myExample">
<div id="exampleBlurb">
<p>
This is an example of an HTML5 form with
<code>required</code> and <code>validate</code> attributes that also uses
<a href="/2010/06/20/visibleif-html5-custom-data-attributes-with-javascript-make-dynamic-interactive-forms/"><code>visibleIf</code></a>
to dynamically hide and show form fields according to other form
field values (in this case, the form will show a Postal Code
field if the country chosen is Canada, or a zip code field if the country chosen is the United States. Note that the validation only happens on those fields that
are visible, since the <code>visibleIf</code> library sets them to be <code>disabled</code>, which prevents validation.
<a href="http://www.useragentman.com/blog/?p=1110">Back to User Agent Man HTML5 Forms article </a>
</p>
</div>
<form action="http://www.useragentman.com/testForm.php">
<fieldset>
<legend>Personal Information</legend>
<p>Thank you for giving to the Zoltan Hawryluk Entertainment Fund. Your
contribution will ensure that Webmasters in Need will be able to buy
beer when they would otherwise do without.</p>
<p>Form fields labelled <img src="images/asterix.gif" alt="*"/> are mandatory</p>
<table class="formTable">
<tr>
<th>Full Name: </th>
<td><input type="text" name="fullName" value="" required="required" />
<div class="description"><strong>Required.</strong> If you would like to give anonymously,
tough. We find people are more generous when their Good
Name is on the line.</div>
</td>
</tr>
<tr>
<th>Company Name: </th>
<td><input type="text" name="companyName" value="" />
<div class="description">If you are unemployed, please leave
blank.</div>
</td>
</tr>
<tr>
<th>Country: </th>
<td><select name="country" >
<option value="Choose one"></option>
<option value="CA">Canada</option>
<option value="US">United States of America</option>
</select>
</td>
</tr>
<tr class="visibleIf" data-visibleIf-rule="country == 'CA'">
<th>Postal Code: </th>
<td>
<input type="text" name="postalCode" value="" required="required"
pattern="[A-Za-z]\d[A-Za-z])\ {0,1}(\d[A-Za-z]\d" />
<div class="description">Please enter a valid Canadian Postal Code.
Valid format is <strong>H0H 0H0</strong> where <strong>H</strong>
is a letter and <strong>0</strong> is a number.</div>
</td>
<tr class="visibleIf" data-visibleIf-rule="country == 'US'">
<th>Zip Code</th>
<td><input type="text" name="postalCode" value="" required="required"
pattern="(\d{5}([\-]\d{4})?)"
/>
<div class="description">Please enter a valid American Postal Code.
Valid format is <strong>nnnnn</strong> or <strong>nnnnn-nnnn</strong>
where <strong>n</strong> is a numeric digit.</div>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Next >"/></td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>