Skip to content

Commit 5bbbec4

Browse files
committed
Pattern attribute / misc
1. Pattern mode deactivates events and Luhn validation 2. Improvements to the QUnit and demo pages 3. Updated README
1 parent a6892fd commit 5bbbec4

File tree

9 files changed

+197
-132
lines changed

9 files changed

+197
-132
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# Release 1.2 (December 28, 2016)
1212

1313
* Now confirmed to work on mobile! See README.md for a list of tested browsers.
14-
* Change to event firing: validation success & validation failure events were firing pre-blur with `preventInvalidInput` enabled. This is contrary to the documentation, which states that these events do not fire in that scenario. The documentation reflected the intended behavior; the events no longer fire in this scenario.
14+
* Change to event firing: validation success & validation failure events were firing pre-blur with `preventInvalidInput` enabled. This was contrary to the documentation, which stated that these events do not fire in that scenario. The documentation reflected the intended behavior; the events no longer fire in this scenario.
1515
* Added credit card types (AMEX, VISA, MasterCard, Discover), plus a credit card type encompassing each of these types
1616
* Added a [Luhn](https://en.wikipedia.org/wiki/Luhn_algorithm) type
1717
* Updated and redesigned the demo page

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
RestrictedTextField v1.4 (UPCOMING RELEASE)
22
========================
33

4-
# You've stumbled upon a development branch. Have a look around if you want, but only use code that's posted on the releases page.
5-
64
See LICENSE for this software's licensing terms.
75

8-
RestrictedTextField is a jQuery plugin which uses regular expressions to validate and control input to HTML text fields. Using 38 built-in types or types you define yourself, it allows you to suppress invalid keystrokes or to allow them into the field but be flagged incorrect. Validation is performed on keystroke, paste and blur.
6+
RestrictedTextField is a jQuery plugin which uses regular expressions to validate and control input to HTML text fields. RestrictedTextField has 38 built-in types and also allows you to define your own types. Validation is performed on keystroke, paste and blur.
7+
8+
You might not need this plugin for your purposes - the HTML5 `pattern` attribute provides most of the same functionality. If, however, you must support browsers that don't support `pattern`, or if you want to make use of RestrictedTextField's additional features, then perhaps it might be a good fit for you.
9+
10+
The biggest difference between this plugin and `pattern` is that RestrcitedTextField allows you to (optionally) suppress invalid keystrokes in real time, rather than allow them into the field to fail validation later. If you do want to allow invalid characters in, however, you have that option.
11+
12+
If you're only interested in `pattern`-based validation, you might still find RestrictedTextField useful. It provides the ability to disable its own validation implementation and use `pattern` exclusively. In this scenario, you'd just be using RestrictedTextField as a repository of pre-written regular expressions. Although you could just copy the regular expressions from the source code, you might find it easier on code maintenance to not be dropping multiple copies of the same regular expressions into your project.
913

1014

1115
## Features
@@ -53,7 +57,7 @@ RestrictedTextField is a jQuery plugin which uses regular expressions to validat
5357
32. MasterCard
5458
33. Discover
5559
34. Credit Card (combines all of the individual credit card types above)
56-
35. [Luhn-valid](https://en.wikipedia.org/wiki/Luhn_algorithm) numbers
60+
35. [Luhn](https://en.wikipedia.org/wiki/Luhn_algorithm) numbers - note: if used with `pattern` mode enabled, successful validation is essentially meaningless.
5761
36. US zip code (5 digits)
5862
37. US zip code suffix (1-4 digits)
5963
38. US zip code (5 digits with optional suffix)
@@ -86,7 +90,7 @@ RestrictedTextField is a jQuery plugin which uses regular expressions to validat
8690

8791
Each credit card type enforces prefixes and length as specified by https://en.wikipedia.org/wiki/Payment_card_number in December, 2016. [Luhn validation](https://en.wikipedia.org/wiki/Luhn_algorithm) is also performed.
8892

89-
If you prefer not to use strictly-enforced credit card types (is Wikipedia correct, and is RestrictedTextField up-to-date?), you can use the Luhn type instead. It performs basic mathematical validation, but it can only tell you that a value is invalid, not that a value IS valid. Full validation would then be shifted to your payment card processor in the form of a rejected transaction -- or, if they provide an API for validation, you could use that.
93+
If you prefer not to use strictly-enforced credit card types (is Wikipedia correct, and is RestrictedTextField up-to-date?), you can use the Luhn type instead. It performs basic mathematical validation, but that will only tell you that a credit card number is invalid, not that it IS valid. Full validation would then be shifted to your payment card processor in the form of a rejected transaction -- or, if they provide an API for validation, you could use that, too.
9094

9195

9296
## Configuration
@@ -96,12 +100,12 @@ If you prefer not to use strictly-enforced credit card types (is Wikipedia corre
96100
| `type` | Text field type. This is a required setting. | string | alpha, upperAlpha, lowerAlpha, alphaSpace, upperAlphaSpace, lowerAlphaSpace, alphanumeric, upperAlphanumeric, lowerAlphanumeric, alphanumericSpace, upperAlphanumericSpace, lowerAlphanumericSpace, int, positiveInt, negativeInt, strictInt, strictPositiveInt, strictNegativeInt, float, positiveFloat, negativeFloat, strictFloat, strictPositiveFloat, strictNegativeFloat, money, positiveMoney, negativeMoney, accountingMoney, negativeAccountingMoney, americanExpress, visa, masterCard, discover, creditCard, luhnNumber, usZip, usZip5, usZipSuffix | null |
97101
| `preventInvalidInput` | When enabled, invalid keystrokes are ignored (the value of the text field is not updated). When disabled, invalid keystrokes are not ignored. | boolean | true/false | true |
98102
| `logger` | An optional callback function for logging. If you want to enable logging, provide a function and then do whatever you wish with the message. | function | A function accepting the log message as a string argument | undefined |
99-
| `usePatternAttr` | When enabled, sets the HTML5 `pattern` attribute on the input field. Check [caniuse.com](http://caniuse.com/#search=pattern) for browser support. | boolean | true/false | false |
103+
| `usePatternAttr` | When enabled, sets the HTML5 `pattern` attribute on the input field. Check [caniuse.com](http://caniuse.com/#search=pattern) for browser support. When pattern mode is enabled, RestrictedTextField does not fire its own validation events, nor does it perform Luhn validation on credit card types or the Luhn type. Pattern mode is meant to be pure HTML5 behavior. The only part of RestrictedTextField functionality that remains active is the automatic formatting of money types.| boolean | true/false | false |
100104

101105

102106
## Events
103107

104-
These events are fired based on the state of the text field.
108+
These events are fired based on the state of the text field, but only if `usePatternAttr` is disabled.
105109

106110
| Event name | Description |
107111
| ------------------| ----------------------------------------------------------------|

demo/demo.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ input.inputIgnored {
2121
}
2222

2323
input.invalid {
24-
border: 2px solid #FF0000;
24+
border: 2px solid #FC8D3D;
2525
}
2626

2727
input[type=submit] {

demo/demo.html

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,9 @@ <h1>Test Drive</h1>
5353
<div id="instructions">
5454
<p>Here you can test drive RestrictedTextField's built-in types. Select a type from the dropdown menu and use the checkboxes to further customize the configuration.</p>
5555

56-
<p>Regardless of the state of the &quot;Discard invalid keystrokes&quot; checkbox, validation is always performed on each keystroke and on blur.</p>
56+
<p>If the <a href="http://caniuse.com/#search=pattern" target="_blank">pattern</a> attribute is disabled, RestrictedTextField uses its own validation implementation: validation is performed on each keystroke and on blur, and is done regardless of the state of the &quot;Discard invalid keystrokes&quot; checkbox. Event listeners flash the text field's border when a keystroke is discarded, and colors its border solid orange if an invalid character makes it into the field.</p>
5757

58-
<p>This demo declares event listeners that flash the text field's border when a keystroke is discarded, and colors its border solid red if an invalid character is allowed into the field.</p>
59-
60-
<p>
61-
To better demonstrate the support of the <a href="http://caniuse.com/#search=pattern" target="_blank">pattern</a> attribute, the event listener which watches for invalid text
62-
inside the field will be disabled when the pattern attribute is in use. You'll notice that with the pattern attribute, the browser's built-in behavior alters the field's border
63-
color, but only on blur. Notice how this differs from RestrictedTextField's approach when the pattern attribute isn't turned on. Note: for types which require validation beyond
64-
a regular expression (for example, a credit card number), the pattern attribute won't provide complete validation; you'll want to heed RestrictedTextField's validation events.
65-
</p>
58+
<p>Note that if using the pattern attribute, you will lose Luhn validation on credit card types and the Luhn type, as Luhn calculations cannot be performed with regular expressions. Patten mode is meant to be a pure HTML5 validation.</p>
6659

6760
<p>The demo initializes RestrictedTextField with a logging function, so you can open your browser's console to view events in real time.</p>
6861
</div>

demo/demo.js

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
if( this.value !== "Select Type" ) {
2525
createField();
2626
} else {
27-
$( "#fieldContainer" ).html( "" );
27+
fieldContainer.hide();
2828
$( "#code" ).html( "" );
2929
}
3030
} );
@@ -42,9 +42,12 @@
4242
var fieldType = document.getElementById( "fieldTypes" ).value;
4343
var preventInvalid = $( "#preventInvalid" ).is( ":checked" );
4444
var usePatternAttr = $( "#usePatternAttr" ).is( ":checked" );
45-
var fieldContainer = document.getElementById( "fieldContainer" );
45+
var fieldContainer = $( "#fieldContainer" );
4646
var field = document.createElement( "input" );
4747
var submit = document.createElement( "input" );
48+
var code = $( "#code" );
49+
50+
var fieldCreated = false;
4851

4952
field.type = "text";
5053
field.id = "field";
@@ -53,9 +56,9 @@
5356
submit.type = "submit";
5457
submit.value = "Submit";
5558

56-
fieldContainer.innerHTML = descrMap[fieldType] + "<br/><br/>";
57-
fieldContainer.appendChild( field );
58-
fieldContainer.appendChild( submit );
59+
fieldContainer.html( descrMap[fieldType] + "<br/><br/>" );
60+
fieldContainer.append( field );
61+
fieldContainer.append( submit );
5962

6063
if( !usePatternAttr ) {
6164
$( submit ).on( "click", function(event) {
@@ -68,47 +71,60 @@
6871

6972
field = $( field );
7073

71-
field.restrictedTextField( {
72-
type : fieldType,
73-
preventInvalidInput : preventInvalid,
74-
usePatternAttr : usePatternAttr,
75-
logger : log
76-
} );
74+
try {
75+
field.restrictedTextField( {
76+
type : fieldType,
77+
preventInvalidInput : preventInvalid,
78+
usePatternAttr : usePatternAttr,
79+
logger : log
80+
} );
7781

78-
field.on( "inputIgnored", function(event) {
79-
var jqThis = $( this );
82+
fieldCreated = true;
83+
} catch( e ) {
84+
fieldContainer.hide();
85+
code.hide();
86+
alert( e );
87+
}
8088

81-
jqThis.removeClass( "invalid" );
82-
jqThis.removeClass( "inputIgnored" );
89+
if( fieldCreated ) {
90+
fieldContainer.show();
91+
code.show();
8392

84-
flashBorder( jqThis, "inputIgnored", 3, 150 );
85-
} )
86-
.on( "validationFailed", function(event) {
87-
if( !usePatternAttr ) {
93+
field.on( "inputIgnored", function(event) {
8894
var jqThis = $( this );
95+
96+
jqThis.removeClass( "invalid" );
8997
jqThis.removeClass( "inputIgnored" );
90-
jqThis.addClass( "invalid" );
91-
}
92-
} )
93-
.on( "validationSuccess", function(event) {
94-
var jqThis = $( this );
9598

96-
jqThis.removeClass( "inputIgnored" );
97-
jqThis.removeClass( "invalid" );
98-
} );
99+
flashBorder( jqThis, "inputIgnored", 3, 150 );
100+
} )
101+
.on( "validationFailed", function(event) {
102+
if( !usePatternAttr ) {
103+
var jqThis = $( this );
104+
jqThis.removeClass( "inputIgnored" );
105+
jqThis.addClass( "invalid" );
106+
}
107+
} )
108+
.on( "validationSuccess", function(event) {
109+
var jqThis = $( this );
110+
111+
jqThis.removeClass( "inputIgnored" );
112+
jqThis.removeClass( "invalid" );
113+
} );
99114

100-
$( "#code" ).html(
101-
"<code>\n" +
102-
"$( \"#field\" ).restrictedTextField( {<br/>\n" +
103-
"&nbsp;&nbsp;&nbsp;type : \"" + fieldType + "\",<br/>\n" +
104-
"&nbsp;&nbsp;&nbsp;preventInvalidInput : " + preventInvalid + ",<br/>\n" +
105-
"&nbsp;&nbsp;&nbsp;usePatternAttr : " + usePatternAttr + ",<br/>\n" +
106-
"&nbsp;&nbsp;&nbsp;logger : log<br/>\n" +
107-
"} );<br/>\n" +
108-
"</code>\n"
109-
);
110-
111-
field.focus();
115+
code.html(
116+
"<code>\n" +
117+
"$( \"#field\" ).restrictedTextField( {<br/>\n" +
118+
"&nbsp;&nbsp;&nbsp;type : \"" + fieldType + "\",<br/>\n" +
119+
"&nbsp;&nbsp;&nbsp;preventInvalidInput : " + preventInvalid + ",<br/>\n" +
120+
"&nbsp;&nbsp;&nbsp;usePatternAttr : " + usePatternAttr + ",<br/>\n" +
121+
"&nbsp;&nbsp;&nbsp;logger : function( msg ) { do stuff }<br/>\n" +
122+
"} );<br/>\n" +
123+
"</code>\n"
124+
);
125+
126+
field.focus();
127+
}
112128
}
113129

114130
function flashBorder( jqField, cssClass, times, delayMillis ) {

0 commit comments

Comments
 (0)