Index: trunk/phase3/resources/mediawiki.specials/mediawiki.specials.preferences.js |
— | — | @@ -41,19 +41,87 @@ |
42 | 42 | |
43 | 43 | // Lame tip to let user know if its email is valid. See bug 22449 |
44 | 44 | $( '#mw-input-emailaddress' ) |
45 | | - .keyup( function() { |
46 | | - var mailtxt = $(this).val(); |
47 | | - if( mailtxt == '' ) { |
48 | | - // mail is optional ! |
49 | | - $(this).removeClass( "invalid" ); |
50 | | - $(this).removeClass( "valid" ); |
51 | | - return; |
| 45 | + .keyup( function () { |
| 46 | + if( $( "#mw-emailaddress-validity" ).length == 0 ) { |
| 47 | + $(this).after( '<label for="mw-input-emailaddress" id="mw-emailaddress-validity"></label>' ); |
52 | 48 | } |
53 | | - if( mailtxt.match( /.+@.+\..+/ ) ) { |
54 | | - $(this).addClass( "valid" ); |
55 | | - $(this).removeClass( "invalid" ); |
56 | | - } else { |
57 | | - $(this).addClass( "invalid" ); |
58 | | - $(this).removeClass( "valid" ); |
59 | | - } |
| 49 | + var isValid = wfValidateEmail( $(this).val() ); |
| 50 | + var class_to_add = isValid ? 'valid' : 'invalid'; |
| 51 | + var class_to_remove = isValid ? 'invalid' : 'valid'; |
| 52 | + $( '#mw-emailaddress-validity' ) |
| 53 | + .text( isValid ? 'Looks valid' : 'Valid address required!' ) |
| 54 | + .addClass( class_to_add ) |
| 55 | + .removeClass( class_to_remove ); |
60 | 56 | } ); |
| 57 | + |
| 58 | +/** |
| 59 | + * Validate a string as representing a valid e-mail address |
| 60 | + * according to HTML5 specification. Please note the specification |
| 61 | + * does not validate a domain with one character. |
| 62 | + * |
| 63 | + * FIXME: should be moved to a JavaScript validation module. |
| 64 | + */ |
| 65 | +wfValidateEmail = function( mailtxt ) { |
| 66 | + if( mailtxt == '' ) { return null; } |
| 67 | + |
| 68 | + /** |
| 69 | + * HTML 5 define a string as valid e-mail address if it matches |
| 70 | + * the ABNF : |
| 71 | + * 1 * ( atext / "." ) "@" ldh-str 1*( "." ldh-str ) |
| 72 | + * With: |
| 73 | + * - atext : defined in RFC 5322 section 3.2.3 |
| 74 | + * - ldh-str : defined in RFC 1034 section 3.5 |
| 75 | + * |
| 76 | + * (see STD 68 / RFC 5234 http://tools.ietf.org/html/std68): |
| 77 | + */ |
| 78 | + |
| 79 | + /** |
| 80 | + * First, define the RFC 5322 'atext' which is pretty easy : |
| 81 | + * atext = ALPHA / DIGIT / ; Printable US-ASCII |
| 82 | + "!" / "#" / ; characters not including |
| 83 | + "$" / "%" / ; specials. Used for atoms. |
| 84 | + "&" / "'" / |
| 85 | + "*" / "+" / |
| 86 | + "-" / "/" / |
| 87 | + "=" / "?" / |
| 88 | + "^" / "_" / |
| 89 | + "`" / "{" / |
| 90 | + "|" / "}" / |
| 91 | + "~" |
| 92 | + */ |
| 93 | + var rfc5322_atext = "a-z0-9!#$%&'*+-/=?^_`{|}—~" ; |
| 94 | + |
| 95 | + /** |
| 96 | + * Next define the RFC 1034 'ldh-str' |
| 97 | + * <domain> ::= <subdomain> | " " |
| 98 | + * <subdomain> ::= <label> | <subdomain> "." <label> |
| 99 | + * <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ] |
| 100 | + * <ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str> |
| 101 | + * <let-dig-hyp> ::= <let-dig> | "-" |
| 102 | + * <let-dig> ::= <letter> | <digit> |
| 103 | + */ |
| 104 | + var rfc1034_ldh_str = "a-z0-9-" ; |
| 105 | + |
| 106 | + var HTML5_email_regexp = new RegExp( |
| 107 | + // start of string |
| 108 | + '^' |
| 109 | + + |
| 110 | + // User part which is liberal :p |
| 111 | + '[' + rfc5322_atext + '\\.' + ']' + '+' |
| 112 | + + |
| 113 | + // "apostrophe" |
| 114 | + '@' |
| 115 | + + |
| 116 | + // Domain first character |
| 117 | + '[' + rfc1034_ldh_str + ']' |
| 118 | + + |
| 119 | + // second char and following can include dot |
| 120 | + '[' + rfc1034_ldh_str + '\\.' + ']' + '+' |
| 121 | + + |
| 122 | + // End of string |
| 123 | + '$', |
| 124 | + // RegExp is case insensitive |
| 125 | + 'i' |
| 126 | + ); |
| 127 | + return mailtxt.match( HTML5_email_regexp ); |
| 128 | +}; |
Index: trunk/phase3/resources/mediawiki.specials/mediawiki.specials.preferences.css |
— | — | @@ -1,8 +1,21 @@ |
2 | | -input.valid { |
| 2 | +#mw-emailaddress-validity { |
| 3 | + padding: 2px 1em; |
| 4 | +} |
| 5 | +body.ltr #mw-emailaddress-validity { |
| 6 | + border-bottom-right-radius:0.8em; |
| 7 | + border-top-right-radius:0.8em; |
| 8 | +} |
| 9 | +body.rtl #mw-emailaddress-validity { |
| 10 | + border-bottom-left-radius:0.8em; |
| 11 | + border-top-left-radius:0.8em; |
| 12 | +} |
| 13 | +#mw-emailaddress-validity.valid { |
| 14 | + border: 1px solid #80FF80; |
3 | 15 | background-color: #C0FFC0; |
4 | 16 | color: black; |
5 | 17 | } |
6 | | -input.invalid { |
| 18 | +#mw-emailaddress-validity.invalid { |
| 19 | + border: 1px solid #FF8080; |
7 | 20 | background-color: #FFC0C0; |
8 | 21 | color: black; |
9 | 22 | } |