2020 < script type ="text/javascript " src ="jquery.restrictedtextfield.js "> </ script >
2121
2222 < style type ="text/css ">
23- input .error {
24- border : 2px solid red;
23+ input .inputIgnored {
24+ border : 2px solid # FC8D3D ;
25+ }
26+
27+ input .invalid {
28+ border : 2px solid # FF0000 ;
2529 }
2630
2731 h2 {
4953 </ style >
5054
5155 < script type ="text/javascript ">
56+ "use strict" ;
57+
5258 function createCustomTypes ( ) {
5359 var cfg = new $ . fn . RestrictedTextFieldConfig ( ) ;
5460
5561 cfg . addType ( "vowels" , / ^ [ a e i o u ] * $ / , null ) ;
5662 cfg . addType ( "additionExpr" , / ^ \d + \+ \d + $ / , / ^ \d + $ | ^ \d + \+ $ / ) ;
5763 }
5864
65+ function flashBorder ( jqField , cssClass , times , delayMillis ) {
66+ if ( times === - 2 ) {
67+ jqField . removeClass ( cssClass ) ;
68+ return ;
69+ }
70+
71+ if ( jqField . hasClass ( cssClass ) ) {
72+ jqField . removeClass ( cssClass ) ;
73+ } else {
74+ jqField . addClass ( cssClass ) ;
75+ }
76+
77+ setTimeout ( function ( ) {
78+ flashBorder ( jqField , cssClass , -- times , delayMillis ) ;
79+ } , delayMillis ) ;
80+ }
81+
5982 $ ( document ) . ready ( function ( ) {
6083 createCustomTypes ( ) ;
6184
6487 preventInvalidInput : false
6588 } ) ;
6689
67- // add listeners for RestrictedTextField events
68-
69- $ ( "#txtAlpha" ) . on ( "validationFailure" , function ( event ) {
70- $ ( this ) . addClass ( "error" ) ;
71- } ) ;
72-
73- $ ( "#txtAlpha" ) . on ( "validationSuccess" , function ( ) {
74- $ ( this ) . removeClass ( "error" ) ;
75- } ) ;
76-
77- // initialize the rest
78-
7990 $ ( "#txtUpperAlpha" ) . restrictedTextField ( { type : "upperAlpha" } ) ;
8091 $ ( "#txtLowerAlpha" ) . restrictedTextField ( { type : "lowerAlpha" } ) ;
8192 $ ( "#txtAlphaSpace" ) . restrictedTextField ( { type : "alphaSpace" } ) ;
8899 $ ( "#txtUpperAlphanumSpace" ) . restrictedTextField ( { type : "upperAlphanumericSpace" } ) ;
89100 $ ( "#txtLowerAlphanumSpace" ) . restrictedTextField ( { type : "lowerAlphanumericSpace" } ) ;
90101 $ ( "#txtInt" ) . restrictedTextField ( { type : "int" } ) ;
91- $ ( "#txtPosInt" ) . restrictedTextField ( { type : "positiveInt" } ) ;
102+ $ ( "#txtPosInt" ) . restrictedTextField ( { type : "positiveInt" } ) ;
92103 $ ( "#txtNegInt" ) . restrictedTextField ( { type : "negativeInt" } ) ;
93104 $ ( "#txtFloat" ) . restrictedTextField ( { type : "float" } ) ;
94105 $ ( "#txtPosFloat" ) . restrictedTextField ( { type : "positiveFloat" } ) ;
100111 $ ( "#txtNegAccountingMoney" ) . restrictedTextField ( { type : "negativeAccountingMoney" } ) ;
101112 $ ( "#txtVowelsOnly" ) . restrictedTextField ( { type : "vowels" } ) ;
102113 $ ( "#txtAdditionExpr" ) . restrictedTextField ( { type : "additionExpr" } ) ;
114+
115+ $ ( "input" ) . on ( "inputIgnored" , function ( event ) {
116+ var jqThis = $ ( this ) ;
117+
118+ jqThis . removeClass ( "invalid" ) ;
119+ jqThis . removeClass ( "inputIgnored" ) ;
120+
121+ flashBorder ( jqThis , "inputIgnored" , 3 , 150 ) ;
122+ } )
123+ . on ( "validationFailed" , function ( event ) {
124+ var jqThis = $ ( this ) ;
125+
126+ jqThis . removeClass ( "inputIgnored" ) ;
127+ jqThis . addClass ( "invalid" ) ;
128+ } )
129+ . on ( "validationSuccess" , function ( ) {
130+ var jqThis = $ ( this ) ;
131+
132+ jqThis . removeClass ( "inputIgnored" ) ;
133+ jqThis . removeClass ( "invalid" ) ;
134+ } ) ;
103135 } ) ;
104136 </ script >
105137 </ head >
@@ -109,18 +141,24 @@ <h2>The first rule of programming: never trust your data. If you're submitting
109141
110142 < h4 > Capturing validation failures</ h4 >
111143 < p >
112- This text field has a listener to capture the < code > validationFailure</ code > and < code > validationSuccess</ code >
113- events. This text field has been configured to allow invalid text to remain in the field. The field's border
114- color will be red for as long as the invalid input remains.
144+ This text field has been configured to allow invalid text to remain in the field. When the invalid keystroke
145+ fails validation, the < code > validationFailed</ code > event fires. When the invalid data is removed, the
146+ < code > validationSuccess</ code > event fires. The < code > validationFailed</ code > listener turns the border red,
147+ and it remains red until the < code > validationSuccess</ code > listener turns it off. When the blur event
148+ fires, the field is validated again, and one of these two events will fire.
115149 </ p >
116150 < h4 > Alpha characters (A-Z, case-insensitive)</ h4 >
117151 < input type ="text " id ="txtAlpha " />
118152
119153 < div id ="break ">
120154 < p >
121- The remaining examples prevent invalid text from being entered at all, so although you could capture
122- < code > validationFailure</ code > and < code > validationSuccess</ code > , you don't really need to - unless
123- you're planning on calling attention to the fact that the user's input was ignored.
155+ The remaining examples work differently. Instead of allowing invalid input to remain in the field, the
156+ invalid keystroke is ignored, so the field never has an invalid value. Events work differently in this
157+ scenario. When an invalid keystroke is ignored, the < code > inputIgnored</ code > event fires. The
158+ < code > validationFailed</ code > event does not fire in response to an invalid keystroke. The
159+ < code > inputIgnored</ code > listener flashes the border of the textbox to alert the user that the
160+ keystroke was ignored. When the blur event fires, the field is validated again, and either the
161+ < code > validationFailed</ code > or < code > validationSuccess</ code > event will fire.
124162 </ p >
125163 </ div >
126164
0 commit comments