add US2ConditionRegex and change to easy use US2ValidateTextField #21

Closed
wants to merge 2 commits into
from
@@ -21,6 +21,8 @@
/* End PBXAggregateTarget section */
/* Begin PBXBuildFile section */
+ 0446DF7116A0E22400023267 /* US2ConditionRegex.h in Headers */ = {isa = PBXBuildFile; fileRef = 0446DF6F16A0E22400023267 /* US2ConditionRegex.h */; };
+ 0446DF7216A0E22400023267 /* US2ConditionRegex.m in Sources */ = {isa = PBXBuildFile; fileRef = 0446DF7016A0E22400023267 /* US2ConditionRegex.m */; };
066D89C716413C2500B398B4 /* US2Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 066D89B916413C2500B398B4 /* US2Localizable.strings */; };
2CCA57AF1640F48000EA1D88 /* US2Localization.h in Headers */ = {isa = PBXBuildFile; fileRef = 2CCA57AD1640F48000EA1D88 /* US2Localization.h */; };
4C0DE68814FE4173005D93CB /* US2ConditionShorthandURL.h in Headers */ = {isa = PBXBuildFile; fileRef = 4C0DE68614FE4173005D93CB /* US2ConditionShorthandURL.h */; };
@@ -117,6 +119,8 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
+ 0446DF6F16A0E22400023267 /* US2ConditionRegex.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = US2ConditionRegex.h; sourceTree = "<group>"; };
+ 0446DF7016A0E22400023267 /* US2ConditionRegex.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = US2ConditionRegex.m; sourceTree = "<group>"; };
066D89BA16413C2500B398B4 /* da */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = da; path = da.lproj/US2Localizable.strings; sourceTree = "<group>"; };
066D89BB16413C2500B398B4 /* de */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = de; path = de.lproj/US2Localizable.strings; sourceTree = "<group>"; };
066D89BC16413C2500B398B4 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/US2Localizable.strings; sourceTree = "<group>"; };
@@ -279,6 +283,8 @@
4C59DD491487BD7B0047C698 /* conditions */ = {
isa = PBXGroup;
children = (
+ 0446DF6F16A0E22400023267 /* US2ConditionRegex.h */,
+ 0446DF7016A0E22400023267 /* US2ConditionRegex.m */,
4C59DD4A1487BD7B0047C698 /* US2ConditionAlphabetic.h */,
4C59DD4B1487BD7B0047C698 /* US2ConditionAlphabetic.m */,
4C59DD4C1487BD7B0047C698 /* US2ConditionAlphanumeric.h */,
@@ -442,6 +448,7 @@
DE2FAA6216275CBD007F2FE0 /* US2ConditionNot.h in Headers */,
DE2FAA6816278AF5007F2FE0 /* US2FormValidator.h in Headers */,
2CCA57AF1640F48000EA1D88 /* US2Localization.h in Headers */,
+ 0446DF7116A0E22400023267 /* US2ConditionRegex.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -621,6 +628,7 @@
DE07953D162759E600841F88 /* US2ConditionOr.m in Sources */,
DE07954116275A0800841F88 /* US2ConditionAnd.m in Sources */,
DE2FAA6316275CBD007F2FE0 /* US2ConditionNot.m in Sources */,
+ 0446DF7216A0E22400023267 /* US2ConditionRegex.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -88,6 +88,13 @@
@property (nonatomic, assign) BOOL validateOnFocusLossOnly;
/**
+ Use this property will be automatically added US2ConditionRegex to US2ValidatorTextField.
+
+ It is very convenient to verify the validity of the UITextField.
+*/
+@property (nonatomic, strong) NSString *validateRegex;
+
+/**
Text for validation
*/
- (NSString *) validatableText;
@@ -27,6 +27,7 @@
#import "US2ValidatorTextFieldPrivate.h"
#import "US2Condition.h"
#import "US2Validator.h"
+#import "US2ConditionRegex.h"
@interface US2ValidatorTextField (private)
@@ -41,6 +42,7 @@ @implementation US2ValidatorTextField
@synthesize validator = _validator;
@synthesize shouldAllowViolations = _shouldAllowViolations;
@synthesize validateOnFocusLossOnly = _validateOnFocusLossOnly;
+@synthesize validateRegex = _validateRegex;
@dynamic isValid;
@@ -149,6 +151,17 @@ - (NSString *) validatableText {
return self.text;
}
+#pragma mark - validateRegex
+
+- (void)setValidateRegex:(NSString *)validateRegex {
+ _validateRegex = validateRegex;
+
+ if (_validator) _validator = nil;
+
+ _validator = [[US2Validator alloc] init];
+ [_validator addCondition:[[US2ConditionRegex alloc] initWithRegex:validateRegex]];
+}
+
#pragma mark -
/**
@@ -0,0 +1,29 @@
+//
+// US2ConditionRegex.h
+// US2FormValidationFramework
+//
+// Created by Jack Qiu on 1/12/13.
+// Copyright (c) 2013 ustwo™. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "US2Condition.h"
+/**
+ The US2ConditionRegex checks a string for regular expression.
+
+ *Example:*
+
+ NSString *string = @"1234";
+
+ US2ConditionRegex *regexCondition = [[US2ConditionRegex alloc] initWithRegex:@"^[0-9]{4}$];
+
+ US2Validator *regexValidator = [[US2Validator alloc] init];
+ [regexValidator addCondition:regexCondition];
+ [regexCondition release];
+
+ BOOL isValid = [regexValidator checkConditions:string] == nil; // isValid == YES
+ */
+@interface US2ConditionRegex : US2Condition
+@property (nonatomic, strong) NSString *regex;
+-(US2ConditionRegex *) initWithRegex:(NSString *)regex;
+@end
@@ -0,0 +1,44 @@
+//
+// US2ConditionRegex.m
+// US2FormValidationFramework
+//
+// Created by Jack Qiu on 1/12/13.
+// Copyright (c) 2013 ustwo™. All rights reserved.
+//
+
+#import "US2ConditionRegex.h"
+
+@implementation US2ConditionRegex
+
+- (US2ConditionRegex *) initWithRegex:(NSString *)regex {
+ self = [self init];
+ if (self != nil) self.regex = regex;
+ return self;
+}
+
+- (BOOL)check:(NSString *)string {
+ if (nil == self.regex) return NO;
+
+ if (nil == string)
+ string = [NSString string];
+
+ NSError *error = NULL;
+ NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:self.regex options:0 error:&error];
+ NSUInteger numberOfMatches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, string.length)];
+
+ return numberOfMatches == 1;
+}
+
+#pragma mark - Allow violation
+
+- (BOOL)shouldAllowViolation {
+ return YES;
+}
+
+#pragma mark - Localization
+
+- (NSString *) createLocalizedViolationString {
+ return US2LocalizedString(@"US2KeyConditionViolationRegex", nil);
+}
+
+@end
@@ -37,6 +37,7 @@
#import "US2ConditionOr.h"
#import "US2ConditionAnd.h"
#import "US2ConditionNot.h"
+#import "US2ConditionRegex.h"
@implementation ConditionUnitTests
@@ -454,4 +455,25 @@ - (void)testUS2ConditionStatic
STAssertTrue([conditionRange isKindOfClass: [US2ConditionRange class]], @"Must be correct class.", nil);
}
+- (void)testUS2ConditionRegex {
+ US2ConditionRegex *condition = [[US2ConditionRegex alloc] init];
+
+ STAssertFalse([condition check:@"something"], @"The US2ConditionRegex should respond wtih FALSE and not TRUE");
+
+ condition = [[US2ConditionRegex alloc] initWithRegex:@"^us2"];
+
+ STAssertTrue([condition check:@"us2_some"], @"The US2ConditionRegex should respond with TRUE and not FALSE");
+
+ STAssertFalse([condition check:@"some_us2_some"], @"The US2ConditionRegex should respond with FALSE and not TRUE");
+
+ condition.regex = @"^[0-9]{7}$";
+ STAssertTrue([condition check:@"1234567"], @"The US2ConditionRegex should respond with TRUE and not FALSE");
+
+ condition.regex = @"^[a-zA-Z0-9]{7}$";
+ STAssertTrue([condition check:@"12a4C67"], @"The US2ConditionRegex should respond with TRUE and not FALSE");
+
+ condition.regex = @"^[a-zA-Z0-9]{7}$";
+ STAssertFalse([condition check:@"12/4C67"], @"The US2ConditionRegex should respond with FALSE and not TRUE");
+}
+
@end