1

Are there any methods available to check whether a UITextField contains roman numerals like i,ii,iii,iv...etc???

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
Ratikanta Patra
  • 1,177
  • 7
  • 18

3 Answers3

1

The only thing you can do (but maybe there are some other solution) is to check the UITextField string for those letter :

Bool find = NO;
NSArray *romans = [NSArray arrayWithObjects:@"i", @"ii", @"ii", @"vi", nil]; //Fill the array as you want

for(NSString *str in romans)
{
  if([textField.text rangeOfString:str].location != NSNotFound)
  {
    find = YES;
    break;
  }
}

if(find)
{
  //Roman letter found
}
Ashbay
  • 1,641
  • 13
  • 20
1

You can use a regular expression through NSRegularExpression in any of the UITextFieldDelegate methods to check if your UITextField has a valid roman number. You can see an example of use of NSRegularExpression in: NSRegularExpression validate email

Your regex should be something like the one posted in: How do you match only valid roman numerals with a regular expression?

Community
  • 1
  • 1
atxe
  • 5,029
  • 2
  • 36
  • 50
1

Use Regex + NSPredicate

-(BOOL)isValidForRoman:(NSString *)text
{
    NSString *romanRegex = @"^(?=.)(?i)M*(D?C{0,3}|C[DM])(L?X{0,3}|X[LC])(V?I{0,3}|I[VX])$";
    NSPredicate *romanTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", romanRegex]; 
    return ([romanTest evaluateWithObject:text]);
}

This Regex is for Roman validation.

TheTiger
  • 13,264
  • 3
  • 57
  • 82