js email validation fixed + i18n support
[lhc/web/wiklou.git] / resources / mediawiki.special / mediawiki.special.preferences.js
1 /*
2 * JavaScript for Special:Preferences
3 */
4
5 $( '#prefsubmit' ).attr( 'id', 'prefcontrol' );
6 $( '#preferences' )
7 .addClass( 'jsprefs' )
8 .before( $( '<ul id="preftoc"></ul>' ) )
9 .children( 'fieldset' )
10 .hide()
11 .addClass( 'prefsection' )
12 .children( 'legend' )
13 .addClass( 'mainLegend' )
14 .each( function( i ) {
15 $(this).parent().attr( 'id', 'prefsection-' + i );
16 if ( i === 0 ) {
17 $(this).parent().show();
18 }
19 $( '#preftoc' ).append(
20 $( '<li></li>' )
21 .addClass( i === 0 ? 'selected' : null )
22 .append(
23 $( '<a></a>')
24 .text( $(this).text() )
25 .attr( 'href', '#prefsection-' + i )
26 .mousedown( function( e ) {
27 $(this).parent().parent().find( 'li' ).removeClass( 'selected' );
28 $(this).parent().addClass( 'selected' );
29 e.preventDefault();
30 return false;
31 } )
32 .click( function( e ) {
33 $( '#preferences > fieldset' ).hide();
34 $( '#prefsection-' + i ).show();
35 e.preventDefault();
36 return false;
37 } )
38 )
39 );
40 } );
41
42 // Lame tip to let user know if its email is valid. See bug 22449
43 $( '#mw-input-wpemailaddress' )
44 .keyup( function () {
45 if( $( "#mw-emailaddress-validity" ).length == 0 ) {
46 $(this).after( '<label for="mw-input-wpemailaddress" id="mw-emailaddress-validity"></label>' );
47 }
48 var isValid = wfValidateEmail( $(this).val() );
49 var class_to_add = isValid ? 'valid' : 'invalid';
50 var class_to_remove = isValid ? 'invalid' : 'valid';
51 $( '#mw-emailaddress-validity' )
52 .text(
53 isValid ?
54 mediaWiki.msg( 'email-address-validity-valid' )
55 : mediaWiki.msg( 'email-address-validity-invalid' )
56 )
57 .addClass( class_to_add )
58 .removeClass( class_to_remove );
59 } );
60
61 /**
62 * Validate a string as representing a valid e-mail address
63 * according to HTML5 specification. Please note the specification
64 * does not validate a domain with one character.
65 *
66 * FIXME: should be moved to a JavaScript validation module.
67 */
68 wfValidateEmail = function( mailtxt ) {
69 if( mailtxt == '' ) { return null; }
70
71 /**
72 * HTML 5 define a string as valid e-mail address if it matches
73 * the ABNF :
74 * 1 * ( atext / "." ) "@" ldh-str 1*( "." ldh-str )
75 * With:
76 * - atext : defined in RFC 5322 section 3.2.3
77 * - ldh-str : defined in RFC 1034 section 3.5
78 *
79 * (see STD 68 / RFC 5234 http://tools.ietf.org/html/std68):
80 */
81
82 /**
83 * First, define the RFC 5322 'atext' which is pretty easy :
84 * atext = ALPHA / DIGIT / ; Printable US-ASCII
85 "!" / "#" / ; characters not including
86 "$" / "%" / ; specials. Used for atoms.
87 "&" / "'" /
88 "*" / "+" /
89 "-" / "/" /
90 "=" / "?" /
91 "^" / "_" /
92 "`" / "{" /
93 "|" / "}" /
94 "~"
95 */
96 var rfc5322_atext = "a-z0-9!#$%&'*+-/=?^_`{|}—~" ;
97
98 /**
99 * Next define the RFC 1034 'ldh-str'
100 * <domain> ::= <subdomain> | " "
101 * <subdomain> ::= <label> | <subdomain> "." <label>
102 * <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
103 * <ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
104 * <let-dig-hyp> ::= <let-dig> | "-"
105 * <let-dig> ::= <letter> | <digit>
106 */
107 var rfc1034_ldh_str = "a-z0-9-" ;
108
109 var HTML5_email_regexp = new RegExp(
110 // start of string
111 '^'
112 +
113 // User part which is liberal :p
114 '[' + rfc5322_atext + '\\.' + ']' + '+'
115 +
116 // "apostrophe"
117 '@'
118 +
119 // Domain first part
120 '[' + rfc1034_ldh_str + ']+'
121 +
122 // Second part and following are separated by a dot
123 '(\\.[' + rfc1034_ldh_str + ']+)+'
124 +
125 // End of string
126 '$',
127 // RegExp is case insensitive
128 'i'
129 );
130 return mailtxt.match( HTML5_email_regexp );
131 };