Are there any methods available to check whether a UITextField contains roman numerals like i,ii,iii,iv...etc???
Asked
Active
Viewed 617 times
3 Answers
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
-
Thanks. It works fine for a quick solution. But got to look for an approach thats gonna help in the long run – Ratikanta Patra Oct 23 '12 at 13:39
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?
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