I have 1 UITextfield
for password in my iPhone application.
I want to validate this textfield with the following validation.
SWIFT 5 USING RXSWIFT, a better, neat and reactive approach.
validate password function will be like this, Obviously you can add as many conditions as you want as per your requirement.
func validatePassword(password: String) -> (Bool, String) {
//Minimum 8 characters at least 1 Alphabet and 1 Number:
var tuple: (Bool, String) = (true, "")
var string = "Requires atleast"
if(password.rangeOfCharacter(from: CharacterSet.letters) == nil){
string = "uppercase"
tuple = (false, string)
}
if(password.rangeOfCharacter(from: CharacterSet.decimalDigits) == nil){
string += ", number"
tuple = (false, string)
}
if(password.count < 8 ){
string += ", 8 chars"
tuple = (false, string)
}
return tuple }
func isPasswordValid(in string: String) -> Observable<(Bool, String)> {
return Observable.create { observer -> Disposable in
let tuple = self.validation.validatePasswordForSymbol(password: string)
observer.onNext(tuple)
return Disposables.create()
}
}
You can use aforementioned function in viewModel or VC as per your architecture. Then invoke the same function like below in your VC.
passwordTextField.rx.text
.orEmpty //1
.filter { $0.count >= 1 } //2
.flatMap { self.isPasswordValid(in: $0) }
.subscribe(onNext: { result in
print("Valid password", result)
//update UI here
// result will be like (false, "Requires atleast, 8 chars, number")
}).disposed(by: disposeBag)
You need to write your validation code in this delegate method of UITextField
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Few links that you might want to refer for implementation
how to use regular expression in iOS sdk
iOS TextField Validation
Use the control:isValidObject:
method of the NSTextFieldDelegate
protocol which allows you to validate the value of a NSTextField. Assuming you have all your interface builder bits and pieces configured correctly, you might do something like this:
@interface PreferencesController : NSWindowController <NSTextFieldDelegate> {
IBOutlet NSTextField *username, *password;
}
@end
@implementation PreferencesController
- (BOOL)control:(NSControl *)control isValidObject:(id)object
{
if (control == password) {
// Perform validation and return YES or NO
}
return YES;
}
@end
use a regex (NSRegularExpression class has docs on how to write the patten itself) and then :
- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//delete
if (string.length == 0) {
return YES;
}
if (self.regEx) {
NSMutableString* check = [NSMutableString stringWithString:theTextField.text];
[check replaceCharactersInRange:range withString:string];
NSTextCheckingResult* match = [self.regEx firstMatchInString:check options:0 range:NSMakeRange(0, [check length])];
if (match.range.length != check.length) {
return NO;
}
}
}
Warning: Restricting the input this way is really confusing for users. You type and type and the character you type just doesnt appear!
I'd maybe go with a small red (!) next to the test field but I'd always allow the input itself!
This is how I would do it. The validation should be done at the end when the user has typed in the password and not in between.I will not be using NSRegularExpression
.
-(void)textFieldDidEndEditing:(UITextField *)textField{
int numberofCharacters = 0;
BOOL lowerCaseLetter,upperCaseLetter,digit,specialCharacter = 0;
if([textField.text length] >= 10)
{
for (int i = 0; i < [textfield.text length]; i++)
{
unichar c = [textfield.text characterAtIndex:i];
if(!lowerCaseLetter)
{
lowerCaseLetter = [[NSCharacterSet lowercaseLetterCharacterSet] characterIsMember:c];
}
if(!upperCaseLetter)
{
upperCaseLetter = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:c];
}
if(!digit)
{
digit = [[NSCharacterSet decimalDigitCharacterSet] characterIsMember:c];
}
if(!specialCharacter)
{
specialCharacter = [[NSCharacterSet symbolCharacterSet] characterIsMember:c];
}
}
if(specialCharacter && digit && lowerCaseLetter && upperCaseLetter)
{
//do what u want
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Please Ensure that you have at least one lower case letter, one upper case letter, one digit and one special character"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Please Enter at least 10 password"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}
Hope this helps...
You can verify your password validation using the below function just pass a password string and this will return you BOOL value.
-(BOOL) isPasswordValid:(NSString *)pwd {
NSCharacterSet *upperCaseChars = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLKMNOPQRSTUVWXYZ"];
NSCharacterSet *lowerCaseChars = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyz"];
//NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
if ( [pwd length]<6 || [pwd length]>20 )
return NO; // too long or too short
NSRange rang;
rang = [pwd rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet]];
if ( !rang.length )
return NO; // no letter
rang = [pwd rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]];
if ( !rang.length )
return NO; // no number;
rang = [pwd rangeOfCharacterFromSet:upperCaseChars];
if ( !rang.length )
return NO; // no uppercase letter;
rang = [pwd rangeOfCharacterFromSet:lowerCaseChars];
if ( !rang.length )
return NO; // no lowerCase Chars;
return YES;
}