@@ -305,3 +305,91 @@ test( "rules(), rangelength attribute as array", function() {
305305 rangelength : [ 2 , 3 ]
306306 } ) ;
307307} ) ;
308+
309+ test ( "rules(), normalizer" , function ( ) {
310+ var username = $ ( "#usernamec" ) ,
311+ urlc = $ ( "#urlc" ) ,
312+ lastname = $ ( "#lastnamec" ) ,
313+ v ;
314+
315+ username . val ( "\t\t \r" ) ;
316+ urlc . val ( "" ) ;
317+
318+ v = $ ( "#testForm1clean" ) . validate ( {
319+ rules : {
320+ username : {
321+ required : true ,
322+ // Using the normalizer to trim the value of the element
323+ // before validating it.
324+ normalizer : function ( value ) {
325+ equal ( this , username [ 0 ] , "`this` in the normalizer should be the username element." ) ;
326+
327+ // Trim the value of the input
328+ return $ . trim ( value ) ;
329+ }
330+ } ,
331+ urlc : {
332+ required : true ,
333+ url : true ,
334+ // Using the normalizer to append https:// if it's not
335+ // present on the input value
336+ normalizer : function ( value ) {
337+ equal ( this , urlc [ 0 ] , "`this` in the normalizer should be the urlc element." ) ;
338+
339+ var url = value ;
340+
341+ // Check if it doesn't start with http:// or https:// or ftp://
342+ if ( url && url . substr ( 0 , 7 ) !== "http://" &&
343+ url . substr ( 0 , 8 ) !== "https://" &&
344+ url . substr ( 0 , 6 ) !== "ftp://" ) {
345+ // then prefix with http:// or even https://
346+ url = "https://" + url ;
347+ }
348+
349+ // Return the new url
350+ return url ;
351+ }
352+ } ,
353+ lastname : {
354+ required : true ,
355+ // Using the normalizer to trim the value of the element
356+ // before validating it.
357+ normalizer : function ( value ) {
358+ equal ( this , lastname [ 0 ] , "`this` in the normalizer should be the lastname element." ) ;
359+
360+ // Return null in order to make sure a exception is thrown
361+ // when normalizer returns a non string value.
362+ value = null ;
363+
364+ return value ;
365+ }
366+ }
367+ }
368+ } ) ;
369+
370+ // Validate only the username and the url elements.
371+ username . valid ( ) ;
372+ equal ( v . invalidElements ( ) [ 0 ] , username [ 0 ] , "The username should be invalid" ) ;
373+
374+ urlc . valid ( ) ;
375+ equal ( v . invalidElements ( ) [ 0 ] , urlc [ 0 ] , "The url should be invalid" ) ;
376+
377+ equal ( v . numberOfInvalids ( ) , 2 , "There is two invalid elements" ) ;
378+
379+ username . val ( "something" ) ;
380+ urlc . val ( "google.com" ) ;
381+
382+ username . trigger ( "keyup" ) ;
383+ urlc . trigger ( "keyup" ) ;
384+
385+ equal ( v . numberOfInvalids ( ) , 0 , "All elements are valid" ) ;
386+ equal ( v . size ( ) , 0 , "All elements are valid" ) ;
387+ equal ( v . isValidElement ( urlc [ 0 ] ) , true , "The url element should be valid" ) ;
388+
389+ // Validate the lastname element, which will throw an exception
390+ throws ( function ( ) {
391+ v . check ( lastname [ 0 ] ) ;
392+ } , function ( err ) {
393+ return err . name === "TypeError" && err . message === "The normalizer should return a string value." ;
394+ } , "This should throw a TypeError exception." ) ;
395+ } ) ;
0 commit comments