From ca097a54cf87ead2c35c6ca5e76e58c07b726ed2 Mon Sep 17 00:00:00 2001 From: Guillaume BIENKOWSKI Date: Thu, 14 Nov 2013 12:54:45 +0100 Subject: [PATCH] Added username validation when creating a new account. It also converts the username to lowercase. --- Classes/WizardViewController.m | 54 ++++- Classes/en.lproj/WizardViews.xib | 341 +++++++++++++++++++------------ 2 files changed, 259 insertions(+), 136 deletions(-) diff --git a/Classes/WizardViewController.m b/Classes/WizardViewController.m index 6c33584c4..5d02a1bcb 100644 --- a/Classes/WizardViewController.m +++ b/Classes/WizardViewController.m @@ -33,7 +33,8 @@ typedef enum _ViewElement { ViewElement_Email = 103, ViewElement_Domain = 104, ViewElement_Label = 200, - ViewElement_Error = 201 + ViewElement_Error = 201, + ViewElement_Username_Error = 404 } ViewElement; @implementation WizardViewController @@ -509,6 +510,49 @@ static UICompositeViewDescription *compositeDescription = nil; activeTextField = textField; } +- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string +{ + // only validate the username when creating a new account + if( (textField.tag == ViewElement_Username) && (currentView == createAccountView) ){ + NSRegularExpression *regex = [NSRegularExpression + regularExpressionWithPattern:@"^[a-z0-9-_\\.]*$" + options:NSRegularExpressionCaseInsensitive + error:nil]; + NSArray* matches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])]; + if ([matches count] == 0) { + UILabel* error = [WizardViewController findLabel:ViewElement_Username_Error view:contentView]; + + // show error with fade animation + [error setText:[NSString stringWithFormat:NSLocalizedString(@"Illegal character in username: %@", nil), string]]; + error.alpha = 0; + error.hidden = NO; + [UIView animateWithDuration:0.3 animations:^{ + error.alpha = 1; + }]; + + // hide again in 2s + [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(hideError:) userInfo:nil repeats:NO]; + + + return NO; + } + } + return YES; +} + +- (void)hideError:(NSTimer*)timer { + UILabel* error_label =[WizardViewController findLabel:ViewElement_Username_Error view:contentView]; + if( error_label ) { + [UIView animateWithDuration:0.3 + animations:^{ + error_label.alpha = 0; + } + completion: ^(BOOL finished) { + error_label.hidden = YES; + } + ]; + } +} #pragma mark - Action Functions @@ -530,6 +574,7 @@ static UICompositeViewDescription *compositeDescription = nil; - (IBAction)onCreateAccountClick:(id)sender { [self changeView:createAccountView back:FALSE animation:TRUE]; + } - (IBAction)onConnectAccountClick:(id)sender { @@ -608,12 +653,11 @@ static UICompositeViewDescription *compositeDescription = nil; NSString *password2 = [WizardViewController findTextField:ViewElement_Password2 view:contentView].text; NSString *email = [WizardViewController findTextField:ViewElement_Email view:contentView].text; NSMutableString *errors = [NSMutableString string]; - + int username_length = [[LinphoneManager instance] lpConfigIntForKey:@"username_length" forSection:@"wizard"]; int password_length = [[LinphoneManager instance] lpConfigIntForKey:@"password_length" forSection:@"wizard"]; if ([username length] < username_length) { - [errors appendString:[NSString stringWithFormat:NSLocalizedString(@"The username is too short (minimum %d characters).\n", nil), username_length]]; } @@ -629,7 +673,7 @@ static UICompositeViewDescription *compositeDescription = nil; if(![emailTest evaluateWithObject:email]) { [errors appendString:NSLocalizedString(@"The email is invalid.\n", nil)]; } - + if([errors length]) { UIAlertView* errorView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Check error(s)",nil) message:[errors substringWithRange:NSMakeRange(0, [errors length] - 1)] @@ -638,7 +682,9 @@ static UICompositeViewDescription *compositeDescription = nil; otherButtonTitles:nil,nil]; [errorView show]; [errorView release]; + } else { + username = [username lowercaseString]; NSString *identity = [self identityFromUsername:username]; [self checkUserExist:identity]; } diff --git a/Classes/en.lproj/WizardViews.xib b/Classes/en.lproj/WizardViews.xib index 2cb5a395b..d5ce3abe3 100644 --- a/Classes/en.lproj/WizardViews.xib +++ b/Classes/en.lproj/WizardViews.xib @@ -1,14 +1,14 @@ - 1536 - 12C60 - 2844 - 1187.34 - 625.00 + 1072 + 13A603 + 4514 + 1265 + 695.00 com.apple.InterfaceBuilder.IBCocoaTouchPlugin - 1930 + 3747 IBProxyObject @@ -93,8 +93,8 @@ 3 20 - - Helvetica-Oblique + + HelveticaNeue-MediumItalic 20 16 @@ -122,8 +122,8 @@ 1 12 - - Helvetica + + HelveticaNeue 12 16 @@ -188,7 +188,7 @@ 10 1 - + @@ -207,15 +207,15 @@ 0 0 NO - Create an account on linphone.org - - 2 - MC43MjU0OTAxOTYxIDAuNzY4NjI3NDUxIDAuNzk2MDc4NDMxNAA - 2 MC4zNTY4NjI3NDUxIDAuMzk2MDc4NDMxNCAwLjQzNTI5NDExNzYAA + + 2 + MC43MjU0OTAxOTYxIDAuNzY4NjI3NDUxIDAuNzk2MDc4NDMxNAA + + Create an account on linphone.org 3 MC41AA @@ -232,8 +232,8 @@ 2 13 - - Helvetica-Bold + + HelveticaNeue-Bold 13 16 @@ -255,14 +255,14 @@ 0 0 NO - I have already a linphone.org account - + + I have already a linphone.org account - + @@ -280,14 +280,14 @@ 0 0 NO - I have already a SIP account - + + I have already a SIP account - + {320, 394} @@ -299,7 +299,7 @@ IBCocoaTouchFramework - + 274 @@ -307,6 +307,7 @@ 311 {{31, 34}, {258, 40}} + _NS:9 NO @@ -326,14 +327,15 @@ 10 1 - + 314 {{39, 80}, {240, 44}} - + + _NS:9 NO YES @@ -349,7 +351,36 @@ 0 1 - + + NO + 240 + + + + -2147483334 + {{40, 118}, {240, 14}} + + + + _NS:9 + NO + YES + 7 + 404 + NO + IBCocoaTouchFramework + Invalid character: + + 1 + MSAwIDAAA + + + 0 + 10 + 0 + 1 + + NO 240 @@ -358,6 +389,7 @@ 319 {{32, 140}, {255, 31}} + _NS:9 NO @@ -390,8 +422,8 @@ 1 14 - - Helvetica + + HelveticaNeue 14 16 @@ -401,6 +433,7 @@ 319 {{32, 185}, {255, 31}} + _NS:9 NO @@ -426,13 +459,14 @@ - + 319 {{32, 230}, {255, 31}} + _NS:9 NO @@ -458,13 +492,14 @@ - + 319 {{32, 275}, {255, 31}} + _NS:9 NO @@ -489,13 +524,14 @@ - + 287 {{33, 330}, {255, 50}} + _NS:9 NO @@ -505,9 +541,9 @@ 0 0 NO - Register - + + Register @@ -515,21 +551,23 @@ 2 20 - - Helvetica-Bold + + HelveticaNeue-Bold 20 16 {320, 394} + + _NS:9 IBCocoaTouchFramework - + 274 @@ -537,6 +575,7 @@ 311 {{31, 34}, {258, 40}} + _NS:9 NO @@ -556,13 +595,14 @@ 10 1 - + 314 {{40, 80}, {240, 44}} + _NS:9 NO @@ -579,7 +619,7 @@ 0 1 - + NO 240 @@ -588,6 +628,7 @@ 319 {{32, 185}, {255, 31}} + _NS:9 NO @@ -613,13 +654,14 @@ - + 319 {{32, 140}, {255, 31}} + _NS:9 NO @@ -644,13 +686,14 @@ - + 287 {{32, 330}, {255, 50}} + _NS:9 NO @@ -660,24 +703,26 @@ 0 0 NO - Sign in - + + Sign in - + {320, 394} + + _NS:9 IBCocoaTouchFramework - + 274 @@ -685,6 +730,7 @@ 311 {{31, 34}, {258, 40}} + _NS:9 NO @@ -704,13 +750,14 @@ 10 1 - + 314 {{40, 80}, {240, 44}} + _NS:9 NO @@ -727,7 +774,7 @@ 0 1 - + NO 240 @@ -736,6 +783,7 @@ 319 {{33, 140}, {255, 31}} + _NS:9 NO @@ -760,13 +808,14 @@ - + 319 {{33, 185}, {255, 31}} + _NS:9 NO @@ -792,13 +841,14 @@ - + 319 {{33, 230}, {255, 31}} + _NS:9 NO @@ -823,13 +873,14 @@ - + 287 {{34, 330}, {255, 50}} + _NS:9 NO @@ -839,24 +890,26 @@ 0 0 NO - Sign in - + + Sign in - + {320, 394} + + _NS:9 IBCocoaTouchFramework - + 274 @@ -864,6 +917,7 @@ 311 {{31, 34}, {258, 40}} + _NS:9 NO @@ -883,13 +937,14 @@ 10 1 - + 314 {{40, 80}, {240, 44}} + _NS:9 NO @@ -906,7 +961,7 @@ 0 1 - + NO 240 @@ -915,6 +970,7 @@ -2147483329 {{32, 185}, {255, 31}} + _NS:9 NO @@ -940,13 +996,14 @@ - + -2147483329 {{32, 140}, {255, 31}} + _NS:9 NO @@ -971,13 +1028,14 @@ - + 287 {{32, 330}, {255, 50}} + _NS:9 NO @@ -987,17 +1045,19 @@ 0 0 NO - Check validation - + + Check validation - + {320, 394} + + _NS:9 @@ -1005,6 +1065,7 @@ + NO @@ -1012,7 +1073,7 @@ - 28 + 28 @@ -1020,7 +1081,7 @@ - 40 + 40 @@ -1028,7 +1089,7 @@ - 70 + 70 @@ -1036,7 +1097,7 @@ - 71 + 71 @@ -1044,7 +1105,7 @@ - 72 + 72 @@ -1052,7 +1113,7 @@ - 112 + 112 @@ -1060,7 +1121,7 @@ - 133 + 133 @@ -1068,7 +1129,7 @@ - 134 + 134 @@ -1076,7 +1137,7 @@ - 135 + 135 @@ -1084,7 +1145,7 @@ - 136 + 136 @@ -1093,7 +1154,7 @@ 7 - 42 + 42 @@ -1102,7 +1163,7 @@ 7 - 41 + 41 @@ -1111,7 +1172,7 @@ 7 - 43 + 43 @@ -1119,7 +1180,7 @@ - 86 + 86 @@ -1127,7 +1188,7 @@ - 87 + 87 @@ -1135,7 +1196,7 @@ - 88 + 88 @@ -1144,7 +1205,7 @@ 7 - 115 + 115 @@ -1153,7 +1214,7 @@ 7 - 116 + 116 @@ -1161,7 +1222,7 @@ - 85 + 85 @@ -1169,7 +1230,7 @@ - 81 + 81 @@ -1177,7 +1238,7 @@ - 80 + 80 @@ -1185,7 +1246,7 @@ - 82 + 82 @@ -1193,7 +1254,7 @@ - 83 + 83 @@ -1202,7 +1263,7 @@ 7 - 113 + 113 @@ -1210,7 +1271,7 @@ - 84 + 84 @@ -1219,7 +1280,7 @@ 7 - 114 + 114 @@ -1227,7 +1288,7 @@ - 109 + 109 @@ -1235,30 +1296,30 @@ - 110 + 110 - 0 + 0 - -1 + -1 File's Owner - -2 + -2 - 21 + 21 @@ -1269,19 +1330,19 @@ welcomeView - 19 + 19 iconImage - 22 + 22 label - 33 + 33 @@ -1294,7 +1355,7 @@ choiceView - 44 + 44 @@ -1304,12 +1365,13 @@ + createAccountView - 52 + 52 @@ -1322,7 +1384,7 @@ connectAccountView - 56 + 56 @@ -1336,109 +1398,109 @@ externalAccountView - 58 + 58 label - 59 + 59 usernameField - 60 + 60 passwordField - 61 + 61 domainField - 62 + 62 signInButton - 66 + 66 passwordField - 67 + 67 label - 68 + 68 usernameField - 73 + 73 label - 74 + 74 usernameField - 75 + 75 passwordField - 76 + 76 password2Field - 77 + 77 registerButton - 79 + 79 emailField - 39 + 39 externalAccountButton - 38 + 38 connectAccountButton - 36 + 36 createAccountButton - 101 + 101 @@ -1451,77 +1513,83 @@ validateAccountView - 103 + 103 checkValidationButton - 105 + 105 label - 104 + 104 usernameField - 106 + 106 passwordField - 117 + 117 title - 122 + 122 title - 123 + 123 title - 124 + 124 title - 126 + 126 title - 64 + 64 signInButton - 127 + 127 title - 128 + 128 choiceViewLogoImage + + uHm-NE-GLV + + + invalid_char_error + @@ -1590,12 +1658,12 @@ UILinphoneTextField com.apple.InterfaceBuilder.IBCocoaTouchPlugin + com.apple.InterfaceBuilder.IBCocoaTouchPlugin - 136 @@ -1820,6 +1888,15 @@ 0 IBCocoaTouchFramework + YES + + com.apple.InterfaceBuilder.CocoaTouchPlugin.iPhoneOS + + + + com.apple.InterfaceBuilder.CocoaTouchPlugin.InterfaceBuilder3 + + YES 3 @@ -1828,6 +1905,6 @@ {542, 88} {179, 161} - 1930 + 3747