-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathErrorCheckUtil.m
More file actions
77 lines (59 loc) · 2.96 KB
/
ErrorCheckUtil.m
File metadata and controls
77 lines (59 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
// ErrorCheckUtil.m
// CodeSprint
//
// Created by Vincent Chau on 7/24/16.
// Copyright © 2016 Vincent Chau. All rights reserved.
//
#import "ErrorCheckUtil.h"
@implementation ErrorCheckUtil
- (instancetype)initWith
{
if (self = [super init]) {
}
return self;
}
- (UIAlertController *)checkBadInput:(NSString *)inputText withMessage:(NSString *)message andDismiss:(NSString *)dismissTitle withSuccessMessage:(NSString *)successMessage title:(NSString *)sucessTitle
{
if ([inputText isEqualToString:@""]) {
return [self showAlertWithTitle:@"Error: Blank Input" andMessage:message
andDismissNamed:dismissTitle];
}
NSCharacterSet *charSet = [NSCharacterSet
characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_- "];
charSet = [charSet invertedSet];
NSRange range = [inputText rangeOfCharacterFromSet:charSet];
if (range.location != NSNotFound) {
return [self showAlertWithTitle:@"Invalid Characters" andMessage:@"Please enter a name containing only: [A-Z], [a-z], [0-9], -, _"
andDismissNamed:@"Dismiss"];
}
UIAlertController *success = [self showAlertWithTitle:sucessTitle andMessage:successMessage
andDismissNamed:@"OK"];
return success; // returns nil if valid input
}
- (UIAlertController *)checkBadInputForTextViews:(NSString *)inputText withMessage:(NSString *)message andDismiss:(NSString *)dismissTitle withSuccessMessage:(NSString *)successMessage title:(NSString *)sucessTitle
{
if ([inputText isEqualToString:@""]) {
return [self showAlertWithTitle:@"Error: Blank Input" andMessage:message
andDismissNamed:dismissTitle];
}
NSCharacterSet *charSet = [NSCharacterSet
characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-:!@#$%^&*()-=+{}[];:'/., "];
charSet = [charSet invertedSet];
NSRange range = [inputText rangeOfCharacterFromSet:charSet];
if (range.location != NSNotFound) {
return [self showAlertWithTitle:@"Invalid Characters" andMessage:@"Some invalid characters may have been detected."
andDismissNamed:@"Dismiss"];
}
UIAlertController *success = [self showAlertWithTitle:sucessTitle andMessage:successMessage
andDismissNamed:@"OK"];
return success; // returns nil if valid input
}
- (UIAlertController *)showAlertWithTitle:(NSString *)title andMessage:(NSString *)message andDismissNamed:(NSString *)dismiss
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:dismiss style:UIAlertActionStyleCancel handler:nil];
[alert addAction:cancel];
return alert;
}
@end